find-dependency

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

11 个版本
安装
npm install find-dependency
yarn add find-dependency
pnpm add find-dependency
bun add find-dependency
README

find-dependency

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

If only the import path is given, process.cwd() is used.

You can also skip looking for a global installation.

findDependency('foo', {
  // Both are optional.
  skipGlobal: true,
  cwd: 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.

版本列表
1.3.3 2021-03-19
1.3.2 2021-03-19
1.3.1 2021-03-19
1.3.0 2021-03-11
1.2.2 2020-11-19
1.2.1 2020-11-19
1.2.0 2019-11-03
1.1.0 2017-11-10
1.0.2 2017-11-10
1.0.1 2017-10-18
1.0.0 2017-10-18