{"id":28076792,"url":"https://github.com/jaredhanson/node-parent-require","last_synced_at":"2025-05-13T01:51:54.896Z","repository":{"id":57319181,"uuid":"11966199","full_name":"jaredhanson/node-parent-require","owner":"jaredhanson","description":"Require modules from parent modules.","archived":false,"fork":false,"pushed_at":"2013-08-12T19:48:57.000Z","size":128,"stargazers_count":34,"open_issues_count":3,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-09T00:51:11.283Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaredhanson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-08T02:58:11.000Z","updated_at":"2025-02-01T13:00:06.000Z","dependencies_parsed_at":"2022-08-25T22:42:26.604Z","dependency_job_id":null,"html_url":"https://github.com/jaredhanson/node-parent-require","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fnode-parent-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fnode-parent-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fnode-parent-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fnode-parent-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaredhanson","download_url":"https://codeload.github.com/jaredhanson/node-parent-require/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253856617,"owners_count":21974576,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-13T01:51:54.490Z","updated_at":"2025-05-13T01:51:54.883Z","avatar_url":"https://github.com/jaredhanson.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"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 \u003c[http://jaredhanson.net/](http://jaredhanson.net/)\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fnode-parent-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaredhanson%2Fnode-parent-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fnode-parent-require/lists"}