关于node.js:查找已安装的NPM包的版本

Find the version of an installed npm package

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

这将打印NPM本身的版本:

1
npm -v <package-name>

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

1
npm version <package-name>

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

1
npm view <package-name> version

如何获取已安装的版本?

  • 使用命令=>npm--version获取已安装的npm版本。
  • 试试npm show version
  • 在我的安装中,"npm-v"报告了npm本身的版本。要在注册表中列出包的最新版本,我发现"npm viewversion"可以完成该任务。


npm listfor local packages或globally packages npm list -gfor installed。P></

You can find the version of a package name as by通过ITS特异an argument。for example,npm list gruntwill result中:P></

1
2
projectName@projectVersion /path/to/project/folder
└── grunt@0.4.1

你可以alternatively,只是没有通过在包装npm listRun as an argument to see the name:在你packages versions ofP></

1
2
3
4
5
6
7
8
├─┬ cli-color@0.1.6
│ └── es5-ext@0.7.1
├── coffee-script@1.3.3
├── less@1.3.0
├─┬ sentry@0.1.2
│ ├── file@0.2.1
│ └── underscore@1.3.3
└── uglify-js@1.2.6

  • 在Mac和Linux上,最好添加"grep module_name",以过滤所需的模块版本。尤其是在使用-g全局运行时。例如:"npm list-g_grep express"以获取已安装的Express版本。
  • 如果您想要一个特定的模块,您可以像npm list less-middleware那样运行它。
  • 根据@guya的*nix系统提示,在Windows上,您可以使用PowerShell获得类似的结果:| select-string module_name来过滤模块。或者,如果您使用的是git-bash(或者只是bash),那么您可以使用grep
  • 如果你不记得listnpm ls也可以。事实上,许多NPM命令都有别名,而且,如果键入命令的子字符串,如果该子字符串不含糊,它也可以工作;例如,npm lsnpm listnpm lis都是相同的。如果需要更详细的输出,请尝试npm ll(但可能需要添加--depth=0)。
  • 这总是给"NPM错误!1"对我来说,无论有没有-g
  • 对于Windows,您也可以使用| findstr"module_name"&180;,但是使用上面@juanpaco提供的npm list [-g] 解决方案效果更好。
  • 输出不适合用脚本进行解析。是否真的没有一种方法可以获得一个只是包版本的输出,而不必执行像npm list -g | awk -F@ '// { print $2}'这样的操作?
  • currentliy NPM 5.5.1有一个--可解析选项。试试npm ls --depth=0 --parseable。参见文档。


一个快速的方式找到了什么是packages of installed without their和局部限制使用:is toP></

1
npm list --depth=0

它给你什么样P></

1
2
3
4
5
6
7
8
9
10
11
12
├── bower@0.8.6
├── grunt@0.4.1
├── grunt-bower-requirejs@0.4.3
├── grunt-contrib-clean@0.4.1
├── grunt-contrib-coffee@0.7.0
├── grunt-contrib-copy@0.4.1
├── grunt-contrib-imagemin@0.1.4
├── grunt-contrib-jshint@0.1.1
├── grunt-contrib-livereload@0.1.2
├── grunt-contrib-requirejs@0.4.1
├── grunt-regarde@0.1.1
└── grunt-svgmin@0.1.0

明显的,globally can be done with the same npm list -g --depth=0。P></

this method is You have clearer房子很多packages of installed。P></

which need to find out to be updated packages npm outdated -g --depth=0,你可以使用。P></

  • 它给了我和没有深度的一样的感觉
  • 似乎他们已经在NPM 1.4.6中修复了它。见github.com/npm/npm/issues/4733
  • | grep 合作很好
  • @ygaradon正确,但是--depth=0使它更快,因为它不必递归地加载依赖项。
  • 用于列出全球安装的模块的npm list -g --depth=0


npm view versionthe latest version -返回available on the package。P></

npm list --depth=0- versions of installed without归来的模的限制。P></

npm list- versions of modules和归来的限制。P></

节点和lastly to get版本:node -vP></

  • npm viewversion,转到npm远程注册表,而不是本地文件系统…
  • @Alexandermills是真的,但是在这里避免了另一个搜索。btw、npm vnpm infonpm show都是npm view的别名。


1
npm info YOUR_PACKAGE version

例如P></

1
2
npm info grunt version
0.4.5

  • 这不显示已安装的包版本,只显示最新的可用版本。
  • 同意@tanner semerad。我查看了NPM的文档,以便对其进行清除。npm infonpm view的别名,在npm的文档中,您会发现这是一种状态:这个命令显示一个包的数据,并将其打印到outpd配置引用的流中,默认为stdout。[…]如果未指定,默认版本为"最新"。这就是我投票否决的方式。
  • 显示可用的最新版本,而不是最新安装的版本。否决了
  • 埃多克斯一〔24〕是唯一起作用的人。


我只是usednpm list | grep 和它的工作都大P></

Windows运行:在线P></

npm list | find P></

在PowerShell的运行:P></

npm list | sls P></

  • find版本对我来说不起作用-即使是带引号的字符串,PowerShell也能很好地工作。
  • 对于Windows用户:npm list -g --depth=0 |find""注意双引号


from the root of the package。P></

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

(编辑:我知道你cdinto the need to s home目录模块,如果你不是已经在那里。如果你有npm installinstalled with the模,然后它会在node_modules/)P></

为回答2:编辑杰夫updated as from @ -迪基P></

  • 好极了!比其他答案建议的运行"NPM列表"快得多(~1s而不是~20s)——至少当您准备好了这个代码片段时!(确实应该有一个NPM插件来完成此操作…)
  • node -p"require('./package.json').version"
  • 如果存在这样的范围分隔符,则不会安装实际版本^~


对于本地包

1
npm list --depth=0

对于全局包

1
npm list  -g --depth=0

如果你同意to install JQ,你可以使用npm listof the JSON输出。P></

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

或者,如果你想好详细P></

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

例如:forP></

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

JSON格式也不同,slightly is for the Global packages,知道你need to change the query。P></

例如:forP></

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

如果你是好的(和有足够的nodeinstalled),你可以永远给你什么样:P></

1
echo"console.log(require('./package.json').version);" | node

这将打印the version of the current)。You can also it to modify去疯狂,这样:P></

1
echo"eval('var result='+require('child_process').execSync('npm version',{encoding:'utf8'})); console.log(result.WHATEVER_PACKAGE_NAME);" | node

WHATEVER_PACKAGE_NAMEversion of the Will打印,包装,that is npm version看到的模式。P></


结合上面的一些答案,产生一个超级简单和超级快速的查找。从项目根目录运行。无需将cx1〔0〕放入任何文件夹,只需一行:

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


您还可以通过此命令检查版本。

npm info version

  • 同样,它显示包注册表中可用的最新版本,而不是当前安装的包的版本。
  • @dawidferenczy同意其仅显示最新版本,如npm info httpaction


to see the packages在局部或globally installed,使用这些命令:P></

  • npm listfor local packages或globally packages npm list -gfor installed。
  • npm list --depth=0
  • npm list | sls
  • node -v

  • 你可以使用视图[模块]公共信息模型,NPM [ ] [ ]节目版本,NPM模或模型〔〕V NPM version to check the version on an installed NPM模块。P></

    我想我们的咕噜模0.4.5 version is the:P></

    1
    2
    3
    4
    npm view grunt version => 0.4.5
    npm info grunt version => 0.4.5
    npm show grunt version => 0.4.5
    npm v grunt version    => 0.4.5

    • 所有这些都显示最新的可用版本,而不是当前安装的版本


    尝试用:P></

    1
    npm list --depth 1 --global packagename


    我已经建立了一个可以做到这一点的工具-QNM

    QNM—用于查询node_modules目录的简单CLI实用程序。

    安装时使用:

    1
    npm i --global qnm

    然后运行:

    1
    qnm [module]

    例如:

    1
    2
    3
    4
    5
    6
    7
    8
    > qnm lodash

    lodash
    ├── 4.17.5
    ├─┬ cli-table2
    │ └── 3.10.1
    └─┬ karma
      └── 3.10.1

    也就是说,我们在node_modules的根中安装了lodash,在cli-table2karmanode_modules中安装了另外两个副本。

    它真的很快,有一些很好的功能,比如标签完成和匹配搜索。


    在UNIX(使用的是便携式和sedgrep)一个眼线,归来the version string of a globally - installed(NPM)gfrom to remove the local -pg查询:packages instead)P></

    1
    2
    $ npm ll -pg --depth=0 grunt | grep -o"@.*:" | sed 's/.$//; s/^.//'
    0.4.5
    • 在输出的字符串npm ll茶类:/usr/lib/node_modules/npm:npm@2.14.8:formatted parseable;
    • grepthe value between the command提取物和:@,包容性;
    • the command the characters sedremoves围岩结构。

    我的.bashrc added to thisP></

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    function npmv {
        case $# in # number of arguments passed
        0) v="$(npm -v)" ; #store output from npm -v in variable
            echo"NPM version is: $v"; #can't use single quotes
                                       #${v} would also work
        ;;  
        1) s="$(npm list --depth=0 $1 | grep $1 | cut -d @ -f 2)";
           echo"$s";
        ;;
        2) case"$2" in # second argument
            g) #global|#Syntax to compare bash string to literal
                 s="$(npm list --depth=0 -g $1 | grep $1 | cut -d @ -f 2)";
            echo"$s";
            ;;
            l) #latest
                 npm view $1 version; #npm info $1 version does same thing
           ;;
           *) echo 'Invalid arguments';
           ;;
           esac;
        ;;
        *) echo 'Invalid arguments';
        ;;
        esac;
    }
    export -f npmv

    现在的IS型:have to doP></

    • npmv for the version of NPM EG:NPM version is: 4.2.0
    • npmv > <包装name for the local EG:0.8.08version
    • npmv name > <包装:全球0.8.09G for version EG
    • 包装npmv<>name for latest version EG:0.8.10

    笔记-在线命令模式均值delimit D截止,followed by the f @,然后2均值均值场第二场因为there will be one of the或者端@符号。P></


    你可以试试这个:npm show {package} versionshows the latest version)。如果你是outdated包恩秀,将与npm outdated版本信息。P></

    • 操作程序询问当前安装的包的版本,而不是包注册表中可用的最新版本。


    我们可以使用NPM查看任何承诺(您的模块名)-v

    • 这将显示可用的最新版本,而不是本地目录中的版本。
    • @Christianbundy您可以使用NPM列表--depth=0_grep uuid
    • 我不得不使用npm list --depth=0 | grep uuid | awk '{ print $2; }' | cut -d '@' -f 2,但是的,只要它是一个顶级的依赖关系,这就可以工作。不过,基于最初的问题,我认为情况并不总是这样。


    package.jsonaccess the

    你可以访问package.jsonor the bower.jsonof the package with:P></

    notepad ./node_modules/:packageName/package.jsonP></

    这将打开在notepadwhich has the the package.jsonversion numberof the included in the command:packageName你。P></for example:

    notepad ./node_modules/vue-template-compiler/package.jsonP></

    再见。P></

    • 不仅有更简单的方法可以做到这一点,而且由于semver范围标记,您在package.json中指定的版本可能不是实际安装的版本。也就是说,可以将依赖项指定为版本^1.3.0,但这可以表示版本1.3.0到1.99.99之间的任何内容。


    这是一个简单的问题,应该有一个比我上面看到的更简单的答案。

    要查看已安装的NPM软件包及其版本,命令是npm ls --depth=0,默认情况下,该命令显示本地安装的软件包。要查看全局安装的软件包,请添加-global参数:npm ls --depth=0 -global

    --depth=0返回一个没有依赖项的已安装包的列表,这是您大多数时候想要做的。

    ls是命令名,listls的别名。