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

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的11个中间版本)
第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 命令检查已安装的依赖关系和版本,并与当前源中的最新版本进行比较。
=== 内置工具 ===
原生的 npm 命令 [https://docs.npmjs.com/cli/v8/commands/npm-outdated outdated] 检查已安装的依赖关系和版本,并与当前源中的最新版本进行比较。


{{shc|<syntaxhighlight lang="bash">
{{shc_out|<syntaxhighlight lang="bash">
npm outdated</syntaxhighlight>|$|/home/kwy}}
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 update</syntaxhighlight>|$|/home/kwy}}
npm update <name></syntaxhighlight>|$|/home/kwy}}


{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
npm upgrade</syntaxhighlight>|$|/home/kwy}}
npm upgrade</syntaxhighlight>|$|/home/kwy}}


{{Linenum|1.}} 除此之外可以使用 npm-check-updates {{Version date|18.0.1|2025-4-24}} 对依赖进行更细致的分析。
=== Cli 工具 ===
除了 npm 内置的工具之外,社区内的开源工具可以对依赖进行更细致的分析。
 
{{Linenum|1.}} 使用 npm-check-updates {{Version date|18.0.1|2025-4-24}} 分析依赖版本。


{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
第38行: 第88行:
Run ncu -u to upgrade package.json
Run ncu -u to upgrade package.json
</syntaxhighlight>|$|/home/kangkk}}
</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>}}


== 依赖清理 ==
== 依赖清理 ==
第56行: 第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