node.js 查找已安装的 npm 包的版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10972176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:51:31  来源:igfitidea点击:

Find the version of an installed npm package

node.jspackagenpm

提问by Laurent Couvidou

How to find the version of an installed node.js/npm package?

如何查找已安装的 node.js/npm的版本?

This prints the version of npm itself:

这将打印 npm 本身的版本:

npm -v <package-name>

This prints a cryptic error:

这会打印一个神秘的错误:

npm version <package-name>

This prints the package version on the registry(i.e. the latest version available):

这会在注册表上打印包版本(即可用的最新版本):

npm view <package-name> version

How do I get the installed version?

我如何获得已安装的版本

回答by TheHippo

npm listfor local packages or npm list -gfor globally installed packages.

npm list对于本地包或npm list -g全局安装的包。

You can find the version of a specific package by passing its name as an argument. For example, npm list gruntwill result in:

您可以通过将其名称作为参数传递来查找特定包的版本。例如,npm list grunt将导致:

projectName@projectVersion /path/to/project/folder
└── [email protected]

Alternatively, you can just run npm listwithout passing a package name as an argument to see the versions of all your packages:

或者,您可以在npm list不传递包名称作为参数的情况下运行以查看所有包的版本:

├─┬ [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
└── [email protected] 

You can also add --depth=0argument to list installed packages without their dependencies.

您还可以添加--depth=0参数以列出已安装的软件包而不需要它们的依赖项。

回答by Patrik Affentranger

Another quick way of finding out what packages are installed locallyand without their dependenciesis to use:

找出本地安装了哪些软件包并且没有它们的依赖项的另一种快速方法是使用:

npm list --depth=0

Which gives you something like

这给了你类似的东西

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Obviously, the same can be done globally with npm list -g --depth=0.

显然,同样可以在全球范围内使用npm list -g --depth=0.

This method is clearer in case you have installed a lot of packages.

如果您安装了很多软件包,这种方法会更清晰。

To find out which packages need to be updated, you can use npm outdated -g --depth=0.

要找出需要更新的软件包,您可以使用npm outdated -g --depth=0.

回答by Salvador Dali

npm view <package> version- returns the latest available version on the package.

npm view <package> version- 返回包上的最新可用版本。

npm list --depth=0- returns versions of all installed modules without dependencies.

npm list --depth=0- 返回没有依赖项的所有已安装模块的版本。

npm list- returns versions of all modules and dependencies.

npm list- 返回所有模块和依赖项的版本。

And lastly to get node version: node -v

最后获取节点版本: node -v

回答by David Beckwith

npm info YOUR_PACKAGE version

e.g.

例如

npm info grunt version
0.4.5

回答by chrissavage

I just used

我刚用

npm list | grep <package name>

and it worked great

效果很好

On windows run:

在 Windows 上运行:

npm list | find <package name>

In PowerShell run:

在 PowerShell 中运行:

npm list | sls <package name>

回答by Fergie

From the root of the package do:

从包的根目录执行:

node -p "require('./package.json').version"

EDIT: (so you need to cdinto the module's home directory if you are not already there. If you have installed the module with npm install, then it will be under node_modules/<module_name>)

编辑:(所以cd如果你还没有进入模块的主目录,你需要进入模块的主目录。如果你已经安装了模块npm install,那么它将在node_modules/<module_name>

EDIT 2: updated as per answer from @jeff-dickey

编辑 2:根据@jeff-dickey 的回答更新

回答by JoshuaDavid

Combining some of the above answers and produces a super simple and super quick lookup.
Run from project root. No need to cdinto any folder, just 1 line:

结合上面的一些答案,产生一个超级简单和超级快速的查找。
从项目根运行。无需cd进入任何文件夹,只需 1 行:

node -p "require('SOMEPACKAGE/package.json').version"

node -p "require('SOMEPACKAGE/package.json').version"

回答by Sobin Sunny

You can also check the version with this command:

您还可以使用以下命令检查版本:

npm info <package name> version

npm info <package name> version

回答by Farhan Yaseen

For local packages

对于本地包

npm list --depth=0

For Global packages

对于全球包裹

npm list  -g --depth=0

回答by Benoit Blanchon

If you agree to install jq, you can use the JSON output of npm list.

如果您同意安装jq,则可以使用.json的 JSON 输出npm list

npm -j ls <package-name> | jq -r .version

or, if you want to be verbose

或者,如果你想变得冗长

npm --json list <package-name> | jq --raw-output '.version'

For instance:

例如:

$ npm -j ls ghost | jq -r .version
0.4.2

Also, the JSON format is slightly different for global packages, so you'll need to change the query.

此外,全局包的 JSON 格式略有不同,因此您需要更改查询。

For instance:

例如:

$ npm -j -g ls | jq -r .dependencies.ghost.version
0.4.2