对于 2018 年及以后的版本,使用 npm 5 或更高版本:编辑您的package-lock.json
: remove the library from "requires"
section 并将其添加到“依赖项”下。
例如,您希望deglob
包使用glob
包版本3.2.11
而不是当前版本。您打开package-lock.json
并看到:
"deglob": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/deglob/-/deglob-2.1.0.tgz",
"integrity": "sha1-TUSr4W7zLHebSXK9FBqAMlApoUo=",
"requires": {
"find-root": "1.1.0",
"glob": "7.1.2",
"ignore": "3.3.5",
"pkg-config": "1.1.1",
"run-parallel": "1.1.6",
"uniq": "1.0.1"
}
},
"glob": "7.1.2",
从中删除"requires"
,添加"dependencies"
正确的版本:
"deglob": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/deglob/-/deglob-2.1.0.tgz",
"integrity": "sha1-TUSr4W7zLHebSXK9FBqAMlApoUo=",
"requires": {
"find-root": "1.1.0",
"ignore": "3.3.5",
"pkg-config": "1.1.1",
"run-parallel": "1.1.6",
"uniq": "1.0.1"
},
"dependencies": {
"glob": {
"version": "3.2.11"
}
}
},
现在删除您的node_modules
文件夹,运行npm ci
(或npm install
旧版本的 node/npm),它将向该部分添加缺少的"dependencies"
部分。
从 npm cli v8.3.0 (2021-12-09) 开始,这可以使用package.jsonoverrides
字段来解决。如StriplingWarrior 的回答中所述