{"id":16682586,"url":"https://github.com/jchip/require-from-path","last_synced_at":"2025-12-30T01:36:42.555Z","repository":{"id":57354251,"uuid":"156881580","full_name":"jchip/require-from-path","owner":"jchip","description":"create nodejs require from a given path","archived":false,"fork":false,"pushed_at":"2019-01-24T20:29:53.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T07:25:28.404Z","etag":null,"topics":["create","from","path","require"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jchip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-09T15:33:18.000Z","updated_at":"2019-01-24T20:29:55.000Z","dependencies_parsed_at":"2022-09-26T20:20:54.456Z","dependency_job_id":null,"html_url":"https://github.com/jchip/require-from-path","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Frequire-from-path","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Frequire-from-path/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Frequire-from-path/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Frequire-from-path/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jchip","download_url":"https://codeload.github.com/jchip/require-from-path/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243392326,"owners_count":20283565,"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":["create","from","path","require"],"created_at":"2024-10-12T14:08:01.762Z","updated_at":"2025-12-30T01:36:42.528Z","avatar_url":"https://github.com/jchip.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]\n[![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url]\n\n# require from path\n\nCreate `require` that's binded to a directory different from where your code is located.\n\nFor example:\n\n```js\nconst requireFrom = require(\"require-from-path\")(\"/some/other/dir\");\nconst foo = requireFrom(\"./foo.js\");\nconst bar = requireFrom(\"bar\");\n```\n\nAbove code will:\n\n- `require` the file `/some/other/dir/foo.js`\n- Search and `require` the module `bar` starting at `/some/other/dir`.\n\n## Purpose\n\nGiven the directory structure below with two NodeJS apps:\n\n    app1\n    |-+ foo\n    | +-- index.js\n    | +--+ node_modules\n    |    +--+ x\n    |       + ...\n    app2\n    |-+ bar\n    | +-- index.js\n    | +--+ node_modules\n    |    +--+ y\n    |       + ...\n\nWhen you call `require(\"x\")` in `/app1/foo/index.js`, NodeJS will search and find module `x` there.\n\nNow from the same file, if you want to resolve the module `y` under the directory `/app2/bar`, you have to use an absolute or relative path directly pointing to `y`, and you may have to do some searching, probably re-implementing Node's module searching algorithm if you don't know exactly where `y` could be.\n\nHowever, in the file `/app2/bar/index.js`, it can just do `require(\"y\")` and Node would automatically find the module for it, because that file is at the location where `y` is under.\n\nWhat if from the file `/app1/foo/index.js`, you can call `require` as if you were at the directory `/app2/bar`, then you would be able to utilize Node's module searching automatically.\n\nTo achieve this, most other implementations choose to re-implement Node's module searching algorithm.\n\nThis module's approach is to tap into Node's `module` and let it do the work.\n\n## Install\n\n    $ npm install require-from-path --save\n\n## Usage\n\nA single function is exported.\n\n##### `requireFromPath(dir, [request])`\n\n- If you call it with just `dir`, then it returns a `require` function that's been binded to the directory `dir`. You can use it to load any module as if you are at `dir`.\n  - You can also call `require.resolve` with the same effect.\n- If you call it with `dir` and a `request`, then it will load and return the module `request` as if at `dir`.\n\n##### Example\n\n```js\nconst requireFromPath = require(\"require-from-path\");\n\n// get back a require binded to /another/dir\n\nconst requireFromPathAnother = requireFromPath(\"/another/dir/\");\nconst modXPath = requireFromPathAnother.resolve(\"modX\");\nconst modX = requireFromPathAnother(\"modX\");\n\n// load modY at /another/yet/dir directly\n\nconst modY = requireFromPath(\"/another/yet/dir\", \"modY\");\n```\n\n## License\n\nApache-2.0 © [Joel Chen](https://github.com/jchip)\n\n[travis-image]: https://travis-ci.org/jchip/require-from-path.svg?branch=master\n[travis-url]: https://travis-ci.org/jchip/require-from-path\n[npm-image]: https://badge.fury.io/js/require-from-path.svg\n[npm-url]: https://npmjs.org/package/require-from-path\n[daviddm-image]: https://david-dm.org/jchip/require-from-path/status.svg\n[daviddm-url]: https://david-dm.org/jchip/require-from-path\n[daviddm-dev-image]: https://david-dm.org/jchip/require-from-path/dev-status.svg\n[daviddm-dev-url]: https://david-dm.org/jchip/require-from-path?type=dev\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchip%2Frequire-from-path","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjchip%2Frequire-from-path","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchip%2Frequire-from-path/lists"}