Node.js/npm/版本号码:修订间差异
来自康健生活
| 第158行: | 第158行: | ||
npm list --depth=0 --no-workspaces <pkg> | grep <pkg> | cut -d '@' -f2 | 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}} | npm list --depth=0 --no-workspaces | grep <pkg>| awk '{ print $2; }' | cut -d '@' -f 2</syntaxhighlight>|$|/home/kwy}} | ||
== 版本升级 == | |||
脚本 | |||
{{hc|head=update.sh|output=<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
# @link{https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=4387764} | |||
# -e 指示 bash 在任何命令 [1] 具有非零退出状态时立即退出 | |||
# -u 未定义的任何变量的引用($*和$@除外)都是错误的并立即退出 | |||
# -o 防止掩盖管道中的错误 | |||
set -euo pipefail | |||
# IFS 变量(代表内部字段分隔符)控制 Bash 所说的单词拆分 | |||
# 当设置为字符串时,Bash 会将字符串中的每个字符视为分隔单词 | |||
IFS=$'\n\t' | |||
runCheckFun(){ | |||
pkgstr="" | |||
# 接收数组不能使用 $1 | |||
# 传入数组也不能使用 "${pks[*]}" 会被转为字符串 | |||
arr=($*) | |||
var_path=`pwd` | |||
for(( i=0;i<${#arr[@]};i++)) do | |||
# ${#arr[@]}获取数组长度用于循环 | |||
# --no-workspaces 解决wran: npm warn config ignoring workspace config at | |||
# @link{https://github.com/npm/cli/issues/4605} | |||
cur_v=$(node -p "try {require('./node_modules/${arr[i]}/package.json').version} catch (e){''}") | |||
next_v=$(npm view ${arr[i]} dist-tags.latest --no-workspaces) | |||
echo "组件"${arr[i]}"的当前版本:"$cur_v",注册表版本:"$next_v; | |||
if [[ $cur_v != $next_v ]]; then | |||
pkgstr="$pkgstr ${arr[i]}@$next_v" | |||
fi | |||
done; | |||
if [[ -n $pkgstr ]];then | |||
echo "当前目录: "$var_path | |||
echo "运行安装命令: pnpm add $pkgstr" | |||
# 字符串作为命令执行, 避免 $pkgstr 中空格存在导致整体变为目录结构被安装 | |||
str="pnpm i $pkgstr" | |||
eval $str | |||
else | |||
echo "版本相同跳过" | |||
fi | |||
} | |||
# npm config set proxy http://192.168.51.34:808 | |||
# npm config set https-proxy http://192.168.51.34:808 | |||
pks=( | |||
[0]="@postlab/types" | |||
[1]="@postlab/platform" | |||
[2]="@postlab/js-sandbox" | |||
[3]="@postlab/component-rest" | |||
[4]="@postlab/codemirror-input" | |||
[5]="@postlab/component-mqtt" | |||
[6]="@postlab/component-socketio" | |||
[7]="@postlab/component-sse" | |||
[8]="@postlab/component-websocket" | |||
) | |||
cd './packages/postlab-common' | |||
runCheckFun ${pks[*]}; | |||
exit 0 | |||
## npm run-script updateself | |||
</syntaxhighlight>}} | |||
2025年7月27日 (日) 07:02的版本
npm
查看 npm 版本
/home/kwy
npm -v
项目
查看当前项目版本
[jian@kang /home/kwy]$
npm version | grep @ | sed -re "s/\{ '.*': '(.*)',?/\1/g"
'@postlab/common': '2025.4.0',
这个命令的字符串处理不是很干净,需要优化。使用 npm pkg get version 可能是一个更好的选择
[jian@kang /home/kwy]$
npm pkg get version
{
"@postlab/common": "2025.4.0"
}
继续优化,命中版本号码所在行
[jian@kang /home/kwy]$
npm pkg get version | grep @
"@postlab/common": "2025.4.0"
匹配双引号内的字符串,并用版本号码替换整行。使用 sed 命令的语句需要灵活多变,可能还需要根据不同操作系统或 npm 版本的变化变更匹配语句,但正则的本质是不变的。
[jian@kang /home/kwy]$
npm pkg get version | grep @ | sed -re "s/\".*\": \"(.*)\",?/\1/g"
2025.4.0
注册表版本号码
/home/kwy
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
/home/kwy
npm view <pkg> --no-workspaces versions --json \
| grep -E '^ *"' \
| sort -rV \
| head -n 1 \
| sed -E 's/^ *"(.+)",*$/\1/'
测试
[jian@kang /home/kwy]$
npm view @postlab/component-rest --no-workspaces versions --json | grep -E '^ *"' | sort -rV | head -n 1 | sed -E 's/^ *"(.+)",*$/\1/'
0.0.9
本地版本号码
nodejs
借助 nodejs 可以间接获取本地已安装 pkg 的版本号码。
/home/kwy
node -p "require('./node_modules/<pkg>/package.json').version"
# or
node -e "console.log(require('./node_modules/<pkg>/package.json').version);"
但是这里有一个问题就是当 require 路径错误时会抛出异常,如
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
这回影响我们的脚本执行,所以需要异常的捕获处理
/home/kwy
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){''}"
测试
[jian@kang /home/kwy]$
node -e "try {console.log(require('./node_modules/@postlab/component-rest/package.json').version)} catch (e){''}"
0.0.7
这种命令式的变体还有:
/home/kwy
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
# 等
cat
使用 cat 命令
/home/kwy
cat node_modules/<pkg>/package.json | grep version
示例
[jian@kang /home/kwy]$
cat node_modules/@postlab/component-rest/package.json | grep version
"version": "0.0.7",
这种方法不推荐使用,当 cat 路径错误时的异常处理过于繁琐
/home/kwy
if (test -a node_modules/<pkg>/package.json); then cat node_modules/<pkg>/package.json | grep version; fi
jq
/home/kwy
npm -j ls <pkg> | jq -r .version
npm --json list <pkg> | jq --raw-output '.version'
全局
/home/kwy
npm -j -g ls | jq -r .dependencies.<pkg>.version
npm
/home/kwy
npm ls <pkg>
#
npm list <pkg>
如果是在 monorepo 模式下会变得繁琐
[jian@kang /home/kwy]$
npm ls @postlab/component-rest --no-workspaces
@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
如下一些语句是参考而来的,都存在一些 匹配问题,但是正则语法万变不离其宗,三步走,第一步捕获字符串,第二步命中字符串,第三步替换。
/home/kwy
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
版本升级
脚本
update.sh
#!/bin/bash
# @link{https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=4387764}
# -e 指示 bash 在任何命令 [1] 具有非零退出状态时立即退出
# -u 未定义的任何变量的引用($*和$@除外)都是错误的并立即退出
# -o 防止掩盖管道中的错误
set -euo pipefail
# IFS 变量(代表内部字段分隔符)控制 Bash 所说的单词拆分
# 当设置为字符串时,Bash 会将字符串中的每个字符视为分隔单词
IFS=$'\n\t'
runCheckFun(){
pkgstr=""
# 接收数组不能使用 $1
# 传入数组也不能使用 "${pks[*]}" 会被转为字符串
arr=($*)
var_path=`pwd`
for(( i=0;i<${#arr[@]};i++)) do
# ${#arr[@]}获取数组长度用于循环
# --no-workspaces 解决wran: npm warn config ignoring workspace config at
# @link{https://github.com/npm/cli/issues/4605}
cur_v=$(node -p "try {require('./node_modules/${arr[i]}/package.json').version} catch (e){''}")
next_v=$(npm view ${arr[i]} dist-tags.latest --no-workspaces)
echo "组件"${arr[i]}"的当前版本:"$cur_v",注册表版本:"$next_v;
if [[ $cur_v != $next_v ]]; then
pkgstr="$pkgstr ${arr[i]}@$next_v"
fi
done;
if [[ -n $pkgstr ]];then
echo "当前目录: "$var_path
echo "运行安装命令: pnpm add $pkgstr"
# 字符串作为命令执行, 避免 $pkgstr 中空格存在导致整体变为目录结构被安装
str="pnpm i $pkgstr"
eval $str
else
echo "版本相同跳过"
fi
}
# npm config set proxy http://192.168.51.34:808
# npm config set https-proxy http://192.168.51.34:808
pks=(
[0]="@postlab/types"
[1]="@postlab/platform"
[2]="@postlab/js-sandbox"
[3]="@postlab/component-rest"
[4]="@postlab/codemirror-input"
[5]="@postlab/component-mqtt"
[6]="@postlab/component-socketio"
[7]="@postlab/component-sse"
[8]="@postlab/component-websocket"
)
cd './packages/postlab-common'
runCheckFun ${pks[*]};
exit 0
## npm run-script updateself
No categories