module-rooter
v0.4.4
Published
Get the resolved path to the root of a package installed in node_modules and find some files
Downloads
2
Maintainers
Readme
module-rooter
Get the resolved path to the root of a package installed in node_modules and find some files
Install
npm:
$ npm install --save module-rooter
yarn:
$ yarn add module-rooter
Usage
/
└── Users
└── admin
└── project
├── index.js
├── package.json
└── node_modules
├── @user
└── module1
├── index.js
├── package.json
└── node_modules
├── module2
├── src
└── index.js
├── package.json
└── node_modules
└── module3
Using in code
const moduleRooter = require('module-rooter')(require);
let rooter = moduleRooter('@user/module1');
console.log(rooter.root);
//=> '/Users/admin/project/node_modules/@user/module1'
console.log(rooter('index.js').cd);
//=> '/Users/admin/project/node_modules/@user/module1/index.js'
rooter = moduleRooter('module2', 'package.json');
console.log(rooter.root);
//=> '/Users/admin/project/node_modules/module2'
console.log(rooter.cd);
//=> '/Users/admin/project/node_modules/module2/package.json'
rooter = rooter('src', 'index.js');
console.log(rooter.cd);
//=> '/Users/admin/project/node_modules/module2/src/index.js'
console.log(rooter.exist);
//=> true
console.log(rooter.error);
//=> ''
rooter = rooter('src', 'other.js');
console.log(rooter.cd);
//=> '/Users/admin/project/node_modules/module2/src/other.js'
console.log(rooter.exist);
//=> false
console.log(rooter.error);
//=> ''
rooter = moduleRooter('module4');
console.log(rooter.error);
//=> MODULE_NOT_FOUND