Node.js/npm/依赖检查:修订间差异
来自康健生活
| (未显示同一用户的15个中间版本) | |||
| 第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 | npm update <name></syntaxhighlight>|$|/home/kwy}} | ||
{{shc|<syntaxhighlight lang="bash"> | |||
npm upgrade</syntaxhighlight>|$|/home/kwy}} | |||
=== Cli 工具 === | |||
除了 npm 内置的工具之外,社区内的开源工具可以对依赖进行更细致的分析。 | |||
{{Linenum|1.}} 使用 npm-check-updates {{Version date|18.0.1|2025-4-24}} 分析依赖版本。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
npm install -g npm-check-updates</syntaxhighlight>|$|/home/kwy}} | |||
运行命令 | |||
{{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% | |||
@codemirror/autocomplete 6.18.1 → 6.18.6 | |||
@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>}} | |||
== 依赖清理 == | == 依赖清理 == | ||
| 第29行: | 第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 | ||
| 第46行: | 第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$}} | |||
2025年7月18日 (五) 20:04的最新版本
依赖包管理
- dependencies: 生产环境依赖包管理
- devDependencies: 开发环境依赖包管理
- optionalDependencies: 可选择依赖包管理,当可选择依赖下载或安装失败不影响 npm 运行进程,可选择依赖覆盖生产环境依赖。等同于编译语言中的 try catch 机制。
- peerDependencies: 指定当前项目作为 package 被安装时的依赖限定条件
- bundledDependencies: 捆绑依赖包管理
版本
| 0.0.1 | 版本 0.0.1 |
| ^0.0.1 | 版本 0.0.1 兼容的最新版本 |
| ~0.0.1 | 版本 0.0.2、0.0.3 等 |
| ~0.1 | 版本 0.2、0.3 等 |
| 0.1.x | 版本 0.1 的任何补丁版本,<、<=、> |
| >=0.0.1 | 大于或等于版本 0.0.1 |
| 0.0.1 0.0.4 | 版本 0.0.1、0.0.3、0.0.4 |
依赖整理
检查所有包,重新按规则整理,按包的字母顺序整理依赖,对于依赖包指定的包,删除其他包中node_modules私有文件夹下的包。
/home/kwy
npm dedupe
依赖关系
内置工具
原生的 npm 命令 outdated 检查已安装的依赖关系和版本,并与当前源中的最新版本进行比较。
kangjian@kangkk:~/github/kangkk.github.io$
npm outdated
Package Current Wanted Latest Location
async MISSING 2.0.1 3.2.0 kangkk
caniuse-api MISSING 1.5.1 3.0.0 kangkk
// ...
目录的方式展示
kangjian@kangkk:~/github/kangkk.github.io$
npm outdated --parseable --depth=0
/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
// ...
通过cut命令就可以得到最后要升级版本号和包名
kangjian@kangkk:~/github/kangkk.github.io$
npm outdated --parseable --depth=0 | cut -d: -f2
async@2.0.1
caniuse-api@1.5.1
// ...
依赖升级
/home/kwy
npm update <name>
/home/kwy
npm upgrade
Cli 工具
除了 npm 内置的工具之外,社区内的开源工具可以对依赖进行更细致的分析。
1. 使用 npm-check-updates version:18.0.1date:2025-4-24 分析依赖版本。
/home/kwy
npm install -g npm-check-updates
运行命令
[jian@kang /home/kangkk]$
ncu
Using pnpm
Checking /home/kwy/postlab-core/packages/codemirror-input/package.json
[====================] 70/70 100%
@codemirror/autocomplete 6.18.1 → 6.18.6
@codemirror/lang-javascript 6.2.2 → 6.2.4
// ...
xml-formatter 3.6.3 → 3.6.6
Run ncu -u to upgrade package.json
更新 package.json 包版本。
[jian@kang /home/kangkk]$
ncu -u
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.
2. 用 shell 脚本管理包升级。
npm-upgrade.sh
#!/bin/sh
set -e
#set -x
for package in $(npm outdated --parseable --depth=0 | cut -d: -f2)
do
npm -g install "$package"
done
依赖清理
依赖的清理有几种常见场景
1. 清理 node_modules 中存在,但 package.json 中不存在 的依赖项。
/home/kangkk
npm prune
2. 检查 package.json 中未使用 的依赖项。
/home/kangkk
npm install -g depcheck
运行 depcheck version:1.4.7date:2023-10-17 来检查未使用的依赖
[jian@kang /home/kangkk]$
depcheck
## or
# npx depcheck
Unused dependencies
* @codemirror/lang-json
// ...
* workbox-window
Unused devDependencies
* @import-meta-env/unplugin
// ...
* vite-plugin-static-copy
3. 检查 package.json 中过时 的依赖项。
/home/kangkk
npm install -g npm-check
运行 npm-check version:6.0.1date:2022-7-17,实测检查内容有些臃肿。类似的还有 npm-check-unused version:6.0.0date:2018-7-2 (forked from dylang/npm-check),所以不做参考,不建议使用。
kangjian@kangkk:~/tongdunol$
npm-check -u -g
❤️ Your modules look amazing. Keep up the great work. ❤️
kangjian@kangkk:~/tongdunol$
npm-check -u
? 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.
No categories