Miss any of our Open RFC calls?Watch the recordings here! »

find-dependency

1.2.0 • Public • Published

find-dependency v1.1.0

Find the path to the package that will be used by require.

const findDependency = require('find-dependency');
 
// Search current directory, all parent directories, and all $NODE_PATH directories.
const packagePath = findDependency('foo', process.cwd());

NOTE: The second argument is optional (defaults to process.cwd()).

How it works

When you call findDependency('foo', __dirname):

  • Check if __dirname/node_modules/foo is a directory
  • Check if __dirname/../node_modules/foo is a directory
  • Continue adding .. until the home directory is checked
  • Check every directory in process.env.NODE_PATH (eg: global_path/foo)
  • At any point, if the checked path exists, return it

This simulates how require resolves the package path, before checking the main field of its package.json file.

New in v1.1.0: No dependencies, and symlinks are followed.

Keywords

none

Install

npm i find-dependency

DownloadsWeekly Downloads

14

Version

1.2.0

License

none

Unpacked Size

3.61 kB

Total Files

5

Last publish

Collaborators

  • avatar