Node.js/npm/依赖检查

来自康健生活
Kangkk​(留言 | 贡献)2025年7月18日 (五) 09:32的版本 (→依赖关系)

依赖包管理

  • dependencies: 生产环境依赖包管理
  • devDependencies: 开发环境依赖包管理
  • optionalDependencies: 可选择依赖包管理,当可选择依赖下载或安装失败不影响 npm 运行进程,可选择依赖覆盖生产环境依赖。等同于编译语言中的 try catch 机制。
  • peerDependencies: 指定当前项目作为 package 被安装时的依赖限定条件
  • bundledDependencies: 捆绑依赖包管理

依赖整理

检查所有包,重新按规则整理,按包的字母顺序整理依赖,对于依赖包指定的包,删除其他包中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

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.

shell

用 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
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.