4.2.0 • Published 5 years ago

resolve-from-npm v4.2.0

Weekly downloads
953
License
ISC
Repository
github
Last release
5 years ago

resolve-from-npm

npm version Build Status codecov

Resolve the path of a module from the directory where npm CLI is installed

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'
})();

Installation

Use npm.

npm install resolve-from-npm

API

const resolveFromNpm = require('resolve-from-npm');

resolveFromNpm(moduleId)

moduleId: string (a module ID)
Return: Promise<string>

It resolves the path of a module from the path where npm-cli-dir resolves.

(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

ISC License © 2017 - 2019 Watanabe Shinnosuke

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.1.0

6 years ago

3.0.0

6 years ago

3.0.0-1

6 years ago

3.0.0-0

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.3-0

6 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

8 years ago

1.0.0

8 years ago

0.0.0

8 years ago