Node.js/npm/依赖检查:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的14个中间版本)
第5行: 第5行:
* peerDependencies: 指定当前项目作为 package 被安装时的依赖限定条件
* peerDependencies: 指定当前项目作为 package 被安装时的依赖限定条件
* bundledDependencies: 捆绑依赖包管理
* bundledDependencies: 捆绑依赖包管理
== 版本 ==
{| class="wikitable"
| <nowiki>0.0.1</nowiki> || 版本 0.0.1
|-
| <nowiki>^0.0.1</nowiki> || 版本 0.0.1 兼容的最新版本
|-
| <nowiki>~0.0.1</nowiki> || 版本 0.0.2、0.0.3 等
|-
| <nowiki>~0.1</nowiki> || 版本 0.2、0.3 等
|-
| <nowiki>0.1.x</nowiki> || 版本 0.1 的任何补丁版本,<nowiki><</nowiki>、<nowiki><=</nowiki>、<nowiki>></nowiki>
|-
| <nowiki>>=0.0.1</nowiki> || 大于或等于版本 0.0.1
|-
| <nowiki>0.0.1 0.0.4</nowiki> || 版本 0.0.1、0.0.3、0.0.4
|}
== 依赖整理 ==
检查所有包,重新按规则整理,按包的字母顺序整理依赖,对于依赖包指定的包,删除其他包中node_modules私有文件夹下的包。
{{shc|<syntaxhighlight lang="bash">
npm dedupe</syntaxhighlight>|$|/home/kwy}}


== 依赖关系 ==
== 依赖关系 ==
检查已安装的依赖关系和版本,并与当前源中的最新版本进行比较。
=== 内置工具 ===
原生的 npm 命令 [https://docs.npmjs.com/cli/v8/commands/npm-outdated outdated] 检查已安装的依赖关系和版本,并与当前源中的最新版本进行比较。
 
{{shc_out|<syntaxhighlight lang="bash">
npm outdated
</syntaxhighlight>|<syntaxhighlight lang="text">
Package          Current  Wanted  Latest  Location
async            MISSING  2.0.1  3.2.0  kangkk
caniuse-api      MISSING  1.5.1  3.0.0  kangkk
// ...</syntaxhighlight>|kangjian@kangkk:~/github/kangkk.github.io$}}
 
目录的方式展示
 
{{shc_out|<syntaxhighlight lang="bash">
npm outdated --parseable --depth=0
</syntaxhighlight>|<syntaxhighlight lang="text">
/home/kangjian/github/kangkk.github.io:async@2.0.1:MISSING:async@3.2.0
/home/kangjian/github/kangkk.github.io:caniuse-api@1.5.1:MISSING:caniuse-api@3.0.0
// ...</syntaxhighlight>|kangjian@kangkk:~/github/kangkk.github.io$}}


通过cut命令就可以得到最后要升级版本号和包名
{{shc_out|<syntaxhighlight lang="bash">
npm outdated --parseable --depth=0 | cut -d: -f2
</syntaxhighlight>|<syntaxhighlight lang="text">
async@2.0.1
caniuse-api@1.5.1
// ...</syntaxhighlight>|kangjian@kangkk:~/github/kangkk.github.io$}}
依赖升级
{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
npm outdated</syntaxhighlight>|$|/home/kangkk}}
npm update <name></syntaxhighlight>|$|/home/kwy}}


{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
npm update</syntaxhighlight>|$|/home/kangkk}}
npm upgrade</syntaxhighlight>|$|/home/kwy}}
 
=== Cli 工具 ===
除了 npm 内置的工具之外,社区内的开源工具可以对依赖进行更细致的分析。
 
{{Linenum|1.}} 使用 npm-check-updates {{Version date|18.0.1|2025-4-24}} 分析依赖版本。


{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
npm upgrade</syntaxhighlight>|$|/home/kangkk}}
npm install -g npm-check-updates</syntaxhighlight>|$|/home/kwy}}
 
运行命令


除了原生的 npm 命令之外,可以安装 cli 工具 npm-check-updates {{Version date|18.0.1|2025-4-24}} 对依赖进行更细致的分析。
{{Shc_out|<syntaxhighlight lang="bash">
ncu</syntaxhighlight>|<syntaxhighlight lang="bash">
Using pnpm
Checking /home/kwy/postlab-core/packages/codemirror-input/package.json
[====================] 70/70 100%


{{shc|<syntaxhighlight lang="bash">
@codemirror/autocomplete              6.18.1  →        6.18.6
npm install -g npm-check-updates</syntaxhighlight>|$|/home/kangkk}}
@codemirror/lang-javascript            6.2.2  →          6.2.4
// ...
xml-formatter                          3.6.3  →          3.6.6
 
Run ncu -u to upgrade package.json
</syntaxhighlight>|$|/home/kangkk}}
 
更新 package.json 包版本。
 
{{Shc_out|<syntaxhighlight lang="bash">
ncu -u</syntaxhighlight>|<syntaxhighlight lang="bash">
Upgrading /Users/kangjian/360/tudou/td-qnpm-backend/package.json
[====================] 58/58 100%
 
agentkeepalive      ~2.0.3  →  ~4.2.1
// ...
 
Run npm install to install new versions.
</syntaxhighlight>|$|/home/kangkk}}
 
{{Linenum|2.}} 用 shell 脚本管理包升级。
 
{{hc|npm-upgrade.sh|<syntaxhighlight lang="shell">
#!/bin/sh
set -e
#set -x
for package in $(npm outdated --parseable --depth=0 | cut -d: -f2)
do
    npm -g install "$package"
done
</syntaxhighlight>}}


== 依赖清理 ==
== 依赖清理 ==
第40行: 第131行:


{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="bash">
depcheck</syntaxhighlight>|<syntaxhighlight lang="bash">
depcheck
## or
# npx depcheck</syntaxhighlight>|<syntaxhighlight lang="bash">
Unused dependencies
Unused dependencies
* @codemirror/lang-json
* @codemirror/lang-json
第57行: 第150行:


运行 npm-check {{Version_date|6.0.1|2022-7-17}},实测检查内容有些臃肿。类似的还有 npm-check-unused {{Version_date|6.0.0|2018-7-2}} (forked from dylang/npm-check),所以不做参考,不建议使用。
运行 npm-check {{Version_date|6.0.1|2022-7-17}},实测检查内容有些臃肿。类似的还有 npm-check-unused {{Version_date|6.0.0|2018-7-2}} (forked from dylang/npm-check),所以不做参考,不建议使用。
{{shc_out|<syntaxhighlight lang="bash">
npm-check -u -g</syntaxhighlight>|<syntaxhighlight lang="text">
❤️  Your modules look amazing. Keep up the great work. ❤️
</syntaxhighlight>|kangjian@kangkk:~/tongdunol$}}
{{shc_out|<syntaxhighlight lang="bash">
npm-check -u</syntaxhighlight>|<syntaxhighlight lang="text">
? Choose which packages to update. (Press <space> to select)
 
Missing. You probably want these.
❯◯ marked-toc missing      0.3.0  https://github.com/jonschlinkert/marked-toc
// ...
 
Minor Update New backwards-compatible features.
◯ moment missing            2.14.1  ❯  2.27.0  https://momentjs.com
// ...
 
Major Update Potentially breaking API changes. Use caution.
◯ async missing            2.0.1  ❯  3.2.0  https://caolan.github.io/async/
// ...
 
Space to select. Enter to start upgrading. Control-C to cancel.
</syntaxhighlight>|kangjian@kangkk:~/tongdunol$}}