{"_id":"parent-require","_rev":"2934525","name":"parent-require","description":"Require modules from parent modules.","dist-tags":{"latest":"1.0.0"},"maintainers":[{"name":"jaredhanson","email":"jaredhanson@gmail.com"}],"time":{"modified":"2023-03-29T06:00:54.000Z","created":"2013-08-12T18:35:45.169Z","1.0.0":"2013-08-12T18:35:45.169Z"},"users":{},"author":{"name":"Jared Hanson","email":"jaredhanson@gmail.com","url":"http://www.jaredhanson.net/"},"repository":{"type":"git","url":"git://github.com/jaredhanson/node-parent-require.git"},"versions":{"1.0.0":{"name":"parent-require","version":"1.0.0","description":"Require modules from parent modules.","keywords":["require","module","modules"],"repository":{"type":"git","url":"git://github.com/jaredhanson/node-parent-require.git"},"bugs":{"url":"http://github.com/jaredhanson/node-parent-require/issues"},"author":{"name":"Jared Hanson","email":"jaredhanson@gmail.com","url":"http://www.jaredhanson.net/"},"licenses":[{"type":"MIT","url":"http://www.opensource.org/licenses/MIT"}],"main":"./index","dependencies":{},"devDependencies":{"mocha":"1.x.x","chai":"1.x.x"},"engines":{"node":">= 0.4.0"},"scripts":{"test":"node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js"},"readmeFilename":"README.md","_id":"parent-require@1.0.0","dist":{"shasum":"746a167638083a860b0eef6732cb27ed46c32977","size":2819,"noattachment":false,"key":"/parent-require/-/parent-require-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/parent-require/download/parent-require-1.0.0.tgz"},"_from":".","_npmVersion":"1.2.25","_npmUser":{"name":"jaredhanson","email":"jaredhanson@gmail.com"},"maintainers":[{"name":"jaredhanson","email":"jaredhanson@gmail.com"}],"directories":{},"publish_time":1376332545169,"_hasShrinkwrap":false,"_cnpm_publish_time":1376332545169,"_cnpmcore_publish_time":"2021-12-16T16:13:18.117Z"}},"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","_attachments":{},"readmeFilename":"README.md","bugs":{"url":"http://github.com/jaredhanson/node-parent-require/issues"}}