{
  "_id": "parent-require",
  "maintainers": [
    {
      "name": "jaredhanson",
      "email": "jaredhanson@gmail.com"
    }
  ],
  "dist-tags": {
    "latest": "1.0.0"
  },
  "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"
      },
      "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",
      "readmeFilename": "README.md",
      "_id": "parent-require@1.0.0",
      "dist": {
        "shasum": "746a167638083a860b0eef6732cb27ed46c32977",
        "tarball": "https://archive.ito.gov.ir/npm/_packages/parent-require/-/parent-require-1.0.0.tgz",
        "integrity": "sha512-2MXDNZC4aXdkkap+rBBMv0lUsfJqvX5/2FiYYnfCnorZt3Pk06/IOR5KeaoghgS2w07MLWgjbsnyaq6PdHn2LQ==",
        "signatures": [
          {
            "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
            "sig": "MEUCIBE9DWW0tWqTuhsajAo1Vit1UGqabSTCnRef1xvuEClsAiEAySfM8T5uPj+f2FCYOUc0FfcKb8Bk7GA0tr/mQY3RiPc="
          }
        ]
      },
      "_from": ".",
      "_npmVersion": "1.2.25",
      "_npmUser": {
        "name": "jaredhanson",
        "email": "jaredhanson@gmail.com"
      },
      "maintainers": [
        {
          "name": "jaredhanson",
          "email": "jaredhanson@gmail.com"
        }
      ]
    }
  },
  "author": {
    "name": "Jared Hanson",
    "email": "jaredhanson@gmail.com",
    "url": "http://www.jaredhanson.net/"
  },
  "_rev": "5-bd820c14d102b8ac5f0dd85e5da890c4",
  "name": "parent-require",
  "description": "Require modules from parent modules.",
  "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",
  "time": {
    "modified": "2026-06-01T10:42:31.965Z",
    "created": "2013-08-12T18:35:43.884Z",
    "1.0.0": "2013-08-12T18:35:45.169Z"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/jaredhanson/node-parent-require.git"
  }
}