{"bugs":{"url":"http://github.com/jaredhanson/node-parent-require/issues"},"dist":{"shasum":"746a167638083a860b0eef6732cb27ed46c32977","tarball":"https://registry.npmjs.org/parent-require/-/parent-require-1.0.0.tgz","integrity":"sha512-2MXDNZC4aXdkkap+rBBMv0lUsfJqvX5/2FiYYnfCnorZt3Pk06/IOR5KeaoghgS2w07MLWgjbsnyaq6PdHn2LQ==","signatures":[{"sig":"MEUCIBE9DWW0tWqTuhsajAo1Vit1UGqabSTCnRef1xvuEClsAiEAySfM8T5uPj+f2FCYOUc0FfcKb8Bk7GA0tr/mQY3RiPc=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"./index","name":"parent-require","_from":".","author":{"url":"http://www.jaredhanson.net/","name":"Jared Hanson","email":"jaredhanson@gmail.com"},"readme":"# parent-require\n\n[![Build](https://travis-ci.org/jaredhanson/node-parent-require.png)](http://travis-ci.org/jaredhanson/node-parent-require)\n[![Coverage](https://coveralls.io/repos/jaredhanson/node-parent-require/badge.png)](https://coveralls.io/r/jaredhanson/node-parent-require)\n[![Dependencies](https://david-dm.org/jaredhanson/node-parent-require.png)](http://david-dm.org/jaredhanson/node-parent-require)\n\n\nRequire modules from parent (i.e. loading) module.\n\n## Install\n\n    $ npm install parent-require\n\n## Usage\n\n`parent-require` addresses an annoying error condition that arises when\ndeveloping plugins, which have [peer dependencies](http://blog.nodejs.org/2013/02/07/peer-dependencies/),\nthat are `npm link`'d into an application.\n\nThe problem is best illustrated by example.  We'll use a shared package of [Mongoose](http://mongoosejs.com/)\nschemas, but the concept applies equally well to any module you plugin to a\nlarger framework.\n\n#### Develop a Plugin for a Framework\n\nLet's develop a set of shared [Mongoose](http://mongoosejs.com/) schemas for a\nuser database, packaged as `mongoose-schemas-users` for reuse by any application\nthat needs to query the database.\n\n```javascript\nvar mongoose = require('mongoose');\n\nvar UserSchema = new mongoose.Schema(...);\n\nmodule.exports = UserSchema;\n```\n\nThe important bit here is that `mongoose` is a *peer dependency* of this\npackage.\n\n#### Require a Plugin from an App\n\nNow, let's install this package...\n\n    npm install mongoose-schemas-users\n\n..and require it within our application:\n\n```javascript\nvar mongoose = require('mongoose')\n  , schemas = require('mongoose-schemas-users')\n  \nmongoose.model('User', schemas.UserSchema);\n```\n\nSo far, so good.\n\n#### npm link Plugin for Development\n\nDuring the course of developing the application, we discover that we need to\ntweak the schemas we've defined.  This is usually easy:\n\n    npm link mongoose-schemas-users\n\nWe've made some edits, and run the application:\n\n    Error: Cannot find module 'mongoose'\n\nWTF?!?  This issue arises because `mongoose` is a *peer dependency*.  Now that\nit has been `npm link`'d to a directory that resides outside of the application\nitself, Node's typical resolution algorithm fails to find it.\n\n#### Fallback to Parent Require\n\nThis is where `parent-require` comes into play.  It provides a fallback to\n`require` modules from the *loading* (aka parent) module.  Because the loading\nmodule exists within the application itself, Node's resolution algorithm will\ncorrectly find our peer dependency.\n\n```javascript\ntry {\n  var mongoose = require('mongoose');\n} catch (_) {\n  // workaround when `npm link`'ed for development\n  var prequire = require('parent-require')\n    , mongoose = prequire('mongoose');\n}\n\nvar UserSchema = new mongoose.Schema(...);\n\nmodule.exports = UserSchema;\n```\n\nWith the fallback in place, we can both `npm install` and `npm link` this\nplugin, correctly resolving peer dependencies in both cases.\n\n## Tests\n\n    $ npm install\n    $ npm test\n\n## Credits\n\n  - [Jared Hanson](http://github.com/jaredhanson)\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\n\nCopyright (c) 2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>\n","engines":{"node":">= 0.4.0"},"scripts":{"test":"node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js"},"_npmUser":{"name":"jaredhanson","email":"jaredhanson@gmail.com"},"keywords":["require","module","modules"],"licenses":[{"url":"http://www.opensource.org/licenses/MIT","type":"MIT"}],"repository":{"url":"git://github.com/jaredhanson/node-parent-require.git","type":"git"},"_npmVersion":"1.2.25","description":"Require modules from parent modules.","maintainers":[{"name":"jaredhanson","email":"jaredhanson@gmail.com"}],"dependencies":{},"readmeFilename":"README.md","devDependencies":{"chai":"1.x.x","mocha":"1.x.x"},"_id":"parent-require@1.0.0","version":"1.0.0"}