解决装包时 Node 版本冲突问题

1,171 阅读2分钟

1. 背景

项目打包失败,报错信息如下:

[19:25:52] error glob@10.4.3: The engine "node" is incompatible with this module. Expected version ">=18". Got "16.14.2"
[19:25:52] error Found incompatible module
[19:25:52] info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
[19:25:52] [ERROR] BUILD ERROR
[19:25:52] [ERROR] 225915098
[19:25:52] [ERROR] 1

可以看到原因为:glob@10.4.3 需要 Node >= 18,机器上的 Node 版本为 16.14.2。

因打包需要 Node16 版本,故解决 glob 问题

2. 过程分析

本地环境的没有问题,感觉是 glob 最近更新了,可以通过 npm info glob 进行查看

glob@11.0.0 | ISC | deps: 6 | versions: 154
the most correct and second fastest glob implementation in JavaScript
https://github.com/isaacs/node-glob#readme

bin: glob

dist
.tarball: http://116.62.114.95:4876/glob/-/glob-11.0.0.tgz
.shasum: 6031df0d7b65eaa1ccb9b29b5ced16cea658e77e
.integrity: sha512-9UiX/Bl6J2yaBbxKoEBRm4Cipxgok8kQYcOPEhScPwebu2I0HoQOuYdIO6S3hLuWoZgpDpwQZMzTFxgpkyT76g==
.unpackedSize: 474.7 kB

dependencies:
foreground-child: ^3.1.0       jackspeak: ^4.0.1              minimatch: ^10.0.0             minipass: ^7.1.2               package-json-from-dist: ^1.0.0 path-scurry: ^2.0.0            

dist-tags:
latest: 11.0.0    legacy: 4.5.3     v7-legacy: 7.2.3  

published 4 hours ago by isaacs <i@izs.me>

昨晚打包报错时版本为 @10.4.3,今天早上又更新了一个大版本 11.0.0,霍~这更新速度。

重新精确查询一下 @10.4.3 版本的时间 npm view glob@10.4.3

glob@10.4.3 | ISC | deps: 6 | versions: 154
the most correct and second fastest glob implementation in JavaScript
https://github.com/isaacs/node-glob#readme

bin: glob

dist
.tarball: http://116.62.114.95:4876/glob/-/glob-10.4.3.tgz
.shasum: e0ba2253dd21b3d0acdfb5d507c59a29f513fc7a
.integrity: sha512-Q38SGlYRpVtDBPSWEylRyctn7uDeTp4NQERTLiCT1FqA9JXPYWqAVmQU6qh4r/zMM5ehxTcbaO8EjhWnvEhmyg==
.unpackedSize: 475.2 kB

dependencies:
foreground-child: ^3.1.0       jackspeak: ^3.1.2              minimatch: ^9.0.4              minipass: ^7.1.2               package-json-from-dist: ^1.0.0 path-scurry: ^1.11.1           

dist-tags:
latest: 11.0.0    legacy: 4.5.3     v7-legacy: 7.2.3  

published 3 days ago by isaacs <i@izs.me>

可以确定是这个包最近更新引起。

回本地开发环境查看 glob 的依赖版本,这里强力推荐工具: qnm,执行 qnm glob

glob 11.0.0 ↰ 4 hours ago | 7.2.3 ↰ 2 years ago
├── 7.2.3 (resolutions) ⇡ 2 years ago (@jest/reporters, cacache, globby...)
└─┬ js-beautify
  └── 10.4.2 (resolutions) ⇡ 2 weeks ago

glob 存在了2个版本

  • 7.2.3 @jest/reporters, cacache, globby...在用
  • 10.4.2 js-beautify 在用

斯~ 这就难了

看一下 js-beautify ,相关依赖比较复杂

➜   qnm js-beautify    
js-beautify 1.15.1 ↰ 4 months ago
└── 1.15.1 ✓ (pretty, vue-jest)
➜   qnm pretty           
pretty 2.0.0 ↰ 7 years ago
└── 2.0.0 ✓ (jest-serializer-vue)
➜   qnm vue-jest      
vue-jest 3.0.7 ↰ 3 years ago
└── 3.0.7 ✓ (@vue/cli-plugin-unit-jest)

看了一下 js-beautify 的源码,没有用 glob 进行骚操作,大概率 7.2.3 hold住。降级用 "glob": "^7.2.3"

js-beautify-sourecode.png

修改 package.json

"resolutions": {
    "glob": "^7.2.3"
}

重新装包,问题解决。

如您有更好的解决方案请评论区留言