View local NPM packages that depend on package X

2019-07-14 03:07发布

I am getting some node-gyp warnings for a particular NPM package, in this case the package is "get-cursor-position". I would like to find out which packages in my local node_modules directory depend on this package. (This might not be easy to do).

If I run:

$ npm view get-cursor-position

I get:

{ name: 'get-cursor-position',
  description: 'Get the cursor\'s current position in your terminal.',
  'dist-tags': { latest: '1.0.3' },
  versions: 
   [ '0.0.1',
     '0.0.2',
     '0.0.4',
     '0.0.5',
     '1.0.0',
     '1.0.1',
     '1.0.2',
     '1.0.3' ],
  maintainers: [ 'bubkoo <bubkoo@163.com>' ],
  time: 
   { modified: '2016-11-01T02:36:07.728Z',
     created: '2016-03-05T03:42:31.517Z',
     '0.0.1': '2016-03-05T03:42:31.517Z',
     '0.0.2': '2016-03-07T00:35:36.627Z',
     '0.0.4': '2016-03-10T07:21:21.364Z',
     '0.0.5': '2016-03-10T07:25:04.846Z',
     '1.0.0': '2016-04-16T08:11:34.546Z',
     '1.0.1': '2016-06-03T15:57:55.767Z',
     '1.0.2': '2016-06-13T14:19:32.966Z',
     '1.0.3': '2016-11-01T02:36:07.728Z' },
  homepage: 'https://github.com/bubkoo/get-cursor-position',
  keywords: [ 'terminal', 'console', 'cursor', 'position', 'ansi', 'escape' ],
  repository: 
   { type: 'git',
     url: 'git+https://github.com/bubkoo/get-cursor-position.git' },
  author: 'bubkoo <bubkoo.wy@gmail.com>',
  bugs: { url: 'https://github.com/bubkoo/get-cursor-position/issues' },
  license: 'MIT',
  readmeFilename: 'README.md',
  version: '1.0.3',
  main: 'index.js',
  scripts: 
   { test: 'echo "Error: no test specified" && exit 1',
     install: 'node-gyp rebuild' },
  gypfile: true,
  gitHead: '56d403bb0e554532d17c403c47421ce8d2db2dec',
  dist: 
   { shasum: '0e41d60343b705836a528d69a5e099e2c5108d63',
     tarball: 'https://registry.npmjs.org/get-cursor-position/-/get-cursor-position-1.0.3.tgz' },
  directories: {} }

I believe npm view will just look up the remote data for the package, I am happy to use the aggregate data on NPM showing all the packages that depend on get-cursor-position, and I can do some manual work on my end to compare with local packages.

I also tried:

npm ls foo

In my case, I tried:

npm ls suman-events

and it didn't seem to pick up what I expected it to pick up. I would have expected it to pick up "suman-example-reporter" in my case =>

As you can see in the image below, "suman-example-reporter" is a direct dependency in my project (it's in package.json) and suman-example-reporter depends on "suman-events" (and "suman-events" is in package.json as well, because it's also a direct dependency of my project).

enter image description here

Anyone know how to do this right?

标签: node.js npm
3条回答
孤傲高冷的网名
2楼-- · 2019-07-14 03:39

The page on npmjs.com lists all the dependent packages. In this case there are only 5 of them.

查看更多
聊天终结者
3楼-- · 2019-07-14 03:42

The npm ls <options> command is definitely what you want.

@Aurora's answer is basically right

Here are the docs for the command:

https://docs.npmjs.com/cli/ls

it says

"When run as ll or la, it shows extended information by default."

So to get a full list of dependencies in your project that depend on x, try:

$ npm la x

instead of

$ npm ls x
查看更多
可以哭但决不认输i
4楼-- · 2019-07-14 03:45

As Ryan suggested in the comments, npm ls <package> will show the dependency tree relating to the specified package, so you can see which packages directly/indirectly require it.

For example, if you install rimraf, once is a dependency, and you can view which package causes it to be installed with:

$ npm ls once
yourpackage@1.0.0 /path/to/pkg
└─┬ rimraf@2.5.4
  └─┬ glob@7.1.1
    └── once@1.4.0

Hence, you can see that once was installed because glob requires it, and rimraf (which I specified in my package.json) depended on glob.

Where extended information is needed, use npm ls --long (or it's shorthand syntax, npm la / npm ll). The extended output will also include the module description, Git repo link, README and will definitely include every module in the tree (some may be skipped with the basic npm ls).

查看更多
登录 后发表回答