resolve-from-npm
Version:
Resolve the path of a module from the directory where npm is installed
62 lines (44 loc) • 1.81 kB
Markdown
[](https://www.npmjs.com/package/resolve-from-npm)
[](https://travis-ci.com/shinnn/resolve-from-npm)
[](https://codecov.io/gh/shinnn/resolve-from-npm)
Resolve the path of a module from the directory where [npm CLI](https://github.com/npm/cli) is installed
```javascript
const resolveFromNpm = require('resolve-from-npm');
require.resolve('node-fetch-npm');
//=> '/path/to/the/current/directory/node_modules/node-fetch-npm/src/index.js'
(async () => {
await resolveFromNpm('node-fetch-npm');
//=> '/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/index.js'
})();
```
[](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install resolve-from-npm
```
```javascript
const resolveFromNpm = require('resolve-from-npm');
```
*moduleId*: `string` (a module ID)
Return: `Promise<string>`
It resolves the path of a module from the path where [npm-cli-dir](https://github.com/shinnn/npm-cli-dir) resolves.
```javascript
(async () => {
await resolveFromNpm('./lib/install'); //=> '/usr/local/lib/node_modules/npm/lib/install.js'
})();
(async () => {
try {
// npm CLI doesn't contain a file './hi'
await resolveFromNpm('./hi');
} catch (err) {
err.code; //=> 'MODULE_NOT_FOUND'
err.message; //=> "Cannot find module './hi' from npm directory '/usr/local/lib/node_modules/npm'."
err.npmDir; //=> '/usr/local/lib/node_modules/npm'
}
})();
```
[](./LICENSE) © 2017 - 2019 Watanabe Shinnosuke