乐闻世界logo
搜索文章和话题

How to list npm user-installed packages

5 个月前提问
3 个月前修改
浏览次数58

6个答案

1
2
3
4
5
6

要列出用户在其本地环境中成功安装的 npm 软件包,您可以执行以下步骤:

  1. 打开命令行界面。

  2. 使用 cd 命令导航到项目的根目录,这样可以确保您查看的是特定项目的依赖项。如果您想查看全局安装的软件包,则可以跳过这一步。

  3. 运行以下命令:

    • 查看项目的本地安装的软件包:
      sh
      npm list
    • 查看全局安装的软件包:
      sh
      npm list -g --depth=0

    这个命令会列出全局安装的顶级(也就是说不包括它们的依赖)软件包。--depth=0 参数确保只列出顶级包,而不是它们所有的依赖项。

  4. 命令执行后,终端会输出一个树形结构,显示所有已安装的软件包及其版本号。

例如,如果您运行 npm list 在一个包含 expresslodash 的项目中,输出可能看起来像这样:

plaintext
project-name@1.0.0 /path/to/project-name ├── express@4.17.1 └── lodash@4.17.20

这表明您的项目成功安装了 express v4.17.1 和 lodash v4.17.20。

还有一些其他的参数可以用来自定义输出内容,例如:

  • --prod 只显示生产环境的依赖。
  • --dev 只显示开发环境的依赖。
  • --json 以 JSON 格式输出结果。

这些命令和参数可以帮助任何使用 npm 的开发者快速查看已安装的软件包信息。

2024年6月29日 12:07 回复

npm list -g --depth=0

  • npm: the Node.js package manager command line tool
  • list -g: display a tree of every package found in the user’s folders (without the -g option it only shows the current directory’s packages)
  • --depth 0 / --depth=0: avoid including every package’s dependencies in the tree view
2024年6月29日 12:07 回复

You can get a list of all globally installed modules using:

shell
ls `npm root -g`
2024年6月29日 12:07 回复

As of 13 December 2015

npm list illustration

While I found the accepted answer 100% correct, and useful, I wished to expand upon it a little based on my own experiences, and hopefully for the benefit of others too. (Here I am using the terms package and module interchangeably)

In an answer to the question, yes the accepted answer would be:

shell
npm list -g --depth=0

You might wish to check for a particular module installed globally, on Unix-like systems or when grep is available. This is particularly useful when checking what version of a module you are using (globally installed; just remove the -g flag if checking a local module):

shell
npm list -g --depth=0 | grep <module_name>

If you'd like to see all available (remote) versions for a particular module, then do:

shell
npm view <module_name> versions

Note, versions is plural. This will give you the full listing of versions to choose from.

For the latest remote version:

shell
npm view <module_name> version

Note, version is singular.

To find out which packages need to be updated, you can use:

shell
npm outdated -g --depth=0

To update global packages, you can use

shell
npm update -g <package>

To update all global packages, you can use:

shell
npm update -g

(However, for npm versions less than 2.6.1, please also see this link as there is a special script that is recommended for globally updating all packages.)

The above commands should work across NPM versions 1.3.x, 1.4.x, 2.x and 3.x.

2024年6月29日 12:07 回复

List NPM packages with some friendly GUI!

This is what I personally prefer and it may be for others too, it may also help during presentations or meetings.

With npm-gui you can list local and global packages with a better visualization.

You can find the package at

Run the following

shell
// Once npm install -g npm-gui cd c:\your-prject-folder npm-gui localhost:9000

Then open your browser at http:\\localhost:9000

npm-gui

2024年6月29日 12:07 回复

For project dependencies use:

shell
npm list --depth=0

For global dependencies use:

shell
npm list -g --depth=0
2024年6月29日 12:07 回复

你的答案