{"id":26844962,"url":"https://github.com/gaafar/pkg-require","last_synced_at":"2025-04-30T20:24:08.019Z","repository":{"id":57325333,"uuid":"89800805","full_name":"Gaafar/pkg-require","owner":"Gaafar","description":"require node files relative to your package directory","archived":false,"fork":false,"pushed_at":"2019-09-18T06:45:05.000Z","size":9,"stargazers_count":22,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T19:37:10.808Z","etag":null,"topics":["module","nodejs","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/Gaafar.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":"2017-04-29T17:19:21.000Z","updated_at":"2021-03-01T06:14:49.000Z","dependencies_parsed_at":"2022-09-09T05:50:18.038Z","dependency_job_id":null,"html_url":"https://github.com/Gaafar/pkg-require","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gaafar%2Fpkg-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gaafar%2Fpkg-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gaafar%2Fpkg-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gaafar%2Fpkg-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gaafar","download_url":"https://codeload.github.com/Gaafar/pkg-require/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251776027,"owners_count":21641930,"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":["module","nodejs","require"],"created_at":"2025-03-30T19:33:56.327Z","updated_at":"2025-04-30T20:24:07.962Z","avatar_url":"https://github.com/Gaafar.png","language":"JavaScript","readme":"[![Build Status](https://travis-ci.org/Gaafar/pkg-require.svg?branch=master)](https://travis-ci.org/Gaafar/pkg-require)\n[![Coverage Status](https://coveralls.io/repos/github/Gaafar/pkg-require/badge.svg?branch=master)](https://coveralls.io/github/Gaafar/pkg-require?branch=master)\n[![npm](https://img.shields.io/npm/v/pkg-require.svg?maxAge=2592000)](https://www.npmjs.com/package/pkg-require)\n[![Dependency Status](https://david-dm.org/Gaafar/pkg-require.svg)](https://david-dm.org/Gaafar/pkg-require)\n\n# pkg-require\n\nrequire node files relative to your package root directory\n\n`npm i -S pkg-require`\n\n## How we got here\n\nImagine this directory tree\n\n```\nroot\n├── foo\n│   └── foo.js\n├── package.json\n└── some\n    └── deep\n        └── dir\n            └── tree\n                └── bar.js\n```\n\nNow if you're in `bar.js` and want to require `foo.js` you need to go all the way up to the root dir and then specify the file you want.\n\n\n```javascript\nconst foo = require('../../../../foo/foo');\n```\n\nThis is annoying for a few reasons:\n* hard to read\n* hard to write\n* needs to be maintained if you move `bar.js` around\n* needs to be maintained if you move `foo.js` around, which cannot be done with a simple search and replace as you may have different numbers of `'../'` in different files requiring `foo.js`\n\nWouldn't it be simpler if you can just write\n\n```javascript\nconst foo = require('foo/foo');\n```\n\nand it would just understand that you mean to require a module relative to your root directory? What do you mean by root directory? The one that contains `package.json`.\n\n# pkg-require\n`pkg-require` will do exactly this. It looks all the way up your directory tree until it finds the first `package.json`, and then it will resolve all `require`s from that directory.\n\n## TLDR Example\n\n```javascript\n// create an instance that will find the nearest parent dir containing package.json from your __dirname\nconst pkgRequire = require('pkg-require')(__dirname);\n\n// require a file relative to the your package.json directory \nconst foo = pkgRequire('foo/foo')\n\n// get the absolute path for a file\nconst absolutePathToFoo = pkgRequire.resolve('foo/foo')\n\n// get the absolute path to your root directory\nconst packageRootPath = pkgRequire.root()\n\n```\nnow you can go write useful code instead of counting how many levels you need to go up to require a file.\n\n## API\n\n### `require`\ncreate a new instance for the package based on current file directory\n```javascript\nconst pkgRequire = require('pkg-require')(__dirname);\n```\n\n### `pkgRequire()`\nrequire a file by passing its path relative to the directory containing `package.json`\n```javascript\nconst foo = pkgRequire('foo/foo')\n```\n\n### `pkgRequire.resolve()`\nresolve a file/dir absolute path by passing its path relative to the directory containing `package.json`\n```javascript\nconst absolutePathToFoo = pkgRequire.resolve('foo/foo')\n```\n\n### `pkgRequire.root()`\nreturn the absolute path to the parent directory containing `package.json`\n```javascript\nconst packageRootPath = pkgRequire.root()\n```\n\n## Other Solutions/Hacks\nThere are a bunch of ways people have been dealing with this problem, which include\n* creating a sym link in `node_modules` to your project dir\n* modifying `$NODE_PATH`\n* calling `require.main.require`\n* using a global variable for base directory\n* overriding/mutating the global `require`\n\nthese solutions are not just hacky and almost impossible to track, most of these solutions will wreak havoc if you use them in package installed with npm, or if you have a few of packages installed doing these hacks.\n\n`pkg-require` does not involve any such hacks, it's a pure function with no side effects or mutations. It takes `__dirname` as an input, finds the first parent directory containing `package.json`, and uses that directory to resolve all files. This way it can work for nested modules and multiple modules because each module will pass its own `__dirname` and get a different instance of the module.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaafar%2Fpkg-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgaafar%2Fpkg-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaafar%2Fpkg-require/lists"}