查看“︁Node.js/npm/版本号码”︁的源代码
来自康健生活
←
Node.js/npm/版本号码
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== npm == 查看 npm 版本 {{shc|<syntaxhighlight lang="bash"> npm -v</syntaxhighlight>|$|/home/kwy}} == 项目 == 查看当前项目版本 {{Shc_out|<syntaxhighlight lang="bash"> npm version | grep @ | sed -re "s/\{ '.*': '(.*)',?/\1/g"</syntaxhighlight>|<syntaxhighlight lang="text"> '@postlab/common': '2025.4.0', </syntaxhighlight>|$|/home/kwy}} 这个命令的字符串处理不是很干净,需要优化。使用 <code>npm pkg get version</code> 可能是一个更好的选择 {{Shc_out|<syntaxhighlight lang="bash"> npm pkg get version</syntaxhighlight>|<syntaxhighlight lang="text">{ "@postlab/common": "2025.4.0" } </syntaxhighlight>|$|/home/kwy}} 继续优化,命中版本号码所在行 {{Shc_out|<syntaxhighlight lang="bash"> npm pkg get version | grep @ </syntaxhighlight>|<syntaxhighlight lang="text"> "@postlab/common": "2025.4.0" </syntaxhighlight>|$|/home/kwy}} 匹配双引号内的字符串,并用版本号码替换整行。使用 sed 命令的语句需要灵活多变,可能还需要根据不同操作系统或 npm 版本的变化变更匹配语句,但正则的本质是不变的。 {{Shc_out|<syntaxhighlight lang="bash"> npm pkg get version | grep @ | sed -re "s/\".*\": \"(.*)\",?/\1/g" </syntaxhighlight>|<syntaxhighlight lang="bash"> 2025.4.0 </syntaxhighlight>|$|/home/kwy}} == 注册表版本号码 == {{shc|<syntaxhighlight lang="bash"> npm info <pkg> version npm view <pkg> version npm show <pkg> version npm view <pkg> versions --json | jq -r '.[-1]' npm view <pkg> versions --json | awk '/"$/{print gensub("[ \"]", "", "G")}' npm v <pkg> version --no-workspaces npm view <pkg> dist-tags.latest</syntaxhighlight>|$|/home/kwy}} {{shc|<syntaxhighlight lang="bash"> npm view <pkg> --no-workspaces versions --json \ | grep -E '^ *"' \ | sort -rV \ | head -n 1 \ | sed -E 's/^ *"(.+)",*$/\1/'</syntaxhighlight>|$|/home/kwy}} 测试 {{Shc_out|<syntaxhighlight lang="bash"> npm view @postlab/component-rest --no-workspaces versions --json | grep -E '^ *"' | sort -rV | head -n 1 | sed -E 's/^ *"(.+)",*$/\1/'</syntaxhighlight>|<syntaxhighlight lang="bash">0.0.9 </syntaxhighlight>|$|/home/kwy}} == 本地版本号码 == === nodejs === 借助 nodejs 可以间接获取本地已安装 pkg 的版本号码。 {{shc|<syntaxhighlight lang="bash"> node -p "require('./node_modules/<pkg>/package.json').version" # or node -e "console.log(require('./node_modules/<pkg>/package.json').version);" </syntaxhighlight>|$|/home/kwy}} 但是这里有一个问题就是当 require 路径错误时会抛出异常,如 <pre> node:internal/modules/cjs/loader:1404 throw err; ^ Error: Cannot find module './node_moddules/@postlab/component-rest/package.json' Require stack: - /home/kwy/postlab-vip/packages/postlab-common/[eval] at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15) // ... requireStack: [ '/home/kwy/postlab-vip/packages/postlab-common/[eval]' ] } Node.js v22.17.0</pre> 这回影响我们的脚本执行,所以需要异常的捕获处理 {{shc|<syntaxhighlight lang="bash"> node -p "try {require('./node_modules/<pkg>/package.json').version} catch (e){''}" # or node -e "try {console.log(require('./node_modules/<pkg>/package.json').version)} catch (e){''}" </syntaxhighlight>|$|/home/kwy}} 测试 {{Shc_out|<syntaxhighlight lang="bash"> node -e "try {console.log(require('./node_modules/@postlab/component-rest/package.json').version)} catch (e){''}" </syntaxhighlight>|<syntaxhighlight lang="text">0.0.7 </syntaxhighlight>|$|/home/kwy}} 这种命令式的变体还有: {{shc|<syntaxhighlight lang="bash"> echo "console.log(require('./package.json').version);" | node # or echo "eval('var result='+require('child_process').execSync('npm version',{encoding:'utf8'})); console.log(result.<pkg>);" | node # 等 </syntaxhighlight>|$|/home/kwy}} === cat === 使用 cat 命令 {{shc|<syntaxhighlight lang="bash"> cat node_modules/<pkg>/package.json | grep version</syntaxhighlight>|$|/home/kwy}} 示例 {{Shc_out|<syntaxhighlight lang="bash"> cat node_modules/@postlab/component-rest/package.json | grep version </syntaxhighlight>|<syntaxhighlight lang="bash"> "version": "0.0.7", </syntaxhighlight>|$|/home/kwy}} 这种方法不推荐使用,当 cat 路径错误时的异常处理过于繁琐 {{shc|<syntaxhighlight lang="bash"> if (test -a node_modules/<pkg>/package.json); then cat node_modules/<pkg>/package.json | grep version; fi</syntaxhighlight>|$|/home/kwy}} === jq === {{shc|<syntaxhighlight lang="bash"> npm -j ls <pkg> | jq -r .version npm --json list <pkg> | jq --raw-output '.version' </syntaxhighlight>|$|/home/kwy}} 全局 {{shc|<syntaxhighlight lang="bash"> npm -j -g ls | jq -r .dependencies.<pkg>.version </syntaxhighlight>|$|/home/kwy}} === npm === {{shc|<syntaxhighlight lang="bash"> npm ls <pkg> # npm list <pkg></syntaxhighlight>|$|/home/kwy}} 如果是在 monorepo 模式下会变得繁琐 {{Shc_out|<syntaxhighlight lang="bash"> npm ls @postlab/component-rest --no-workspaces</syntaxhighlight>|<syntaxhighlight lang="bash"> @postlab/common@2025.4.0 /home/kwy/postlab-vip/packages/postlab-common └── @postlab/component-rest@0.0.7 -> ./../../node_modules/.pnpm/@postlab+component-rest@0.0.7_@lezer+common@1.2.1_eslint@8.57.0_graphql@16.9.0_openapi-_4baa08fda89e43a02ac08d9dc46c167c/node_modules/@postlab/component-rest </syntaxhighlight>|$|/home/kwy}} 如下一些语句是参考而来的,都存在一些 匹配问题,但是正则语法万变不离其宗,三步走,第一步捕获字符串,第二步命中字符串,第三步替换。 {{shc|<syntaxhighlight lang="bash"> npm ll -p --no-workspaces --depth=0 <pkg> | grep -o "@.*:" | sed 's/.$//; s/^.//' npm ll -pg --depth=0 <pkg> | grep -o "@.*:" | sed 's/.$//; s/^.//' npm list --depth=0 --no-workspaces <pkg> | grep <pkg> | cut -d '@' -f2 npm list --depth=0 --no-workspaces | grep <pkg>| awk '{ print $2; }' | cut -d '@' -f 2</syntaxhighlight>|$|/home/kwy}}
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Shc
(
查看源代码
)
模板:Shc/style.css
(
查看源代码
)
模板:Shc out
(
查看源代码
)
模板:Shc out/style.css
(
查看源代码
)
返回
Node.js/npm/版本号码
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。