重建package-lock.json时遇npm ERR! Cannot convert undefined or null错误求助
解决npm重建package-lock.json时出现“Cannot convert undefined or null to object”错误
现有项目的package-lock.json因不明原因被删除,尝试通过npm i --package-lock-only或直接执行npm install重建该文件时,触发以下错误:
npm ERR! Cannot convert undefined or null to object
具体调用栈日志如下:
2843 verbose stack TypeError: Cannot convert undefined or null to object 2843 verbose stack at Function.keys (<anonymous>) 2843 verbose stack at module.exports (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-pick-manifest\lib\index.js:213:22) 2843 verbose stack at RegistryFetcher.manifest (C:\Program Files\nodejs\node_modules\npm\node_modules\pacote\lib\registry.js:119:22) 2843 verbose stack at async Promise.all (index 10) 2843 verbose stack at async [buildDepStep] (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:1044:5) 2843 verbose stack at async Arborist.buildIdealTree (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:207:7) 2843 verbose stack at async Arborist.reify (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\reify.js:159:5) 2843 verbose stack at async Install.exec (C:\Program Files\nodejs\node_modules\npm\lib\commands\install.js:147:5) 2843 verbose stack at async module.exports (C:\Program Files\nodejs\node_modules\npm\lib\cli.js:89:5) $ node --version v18.16.0 $ npm --version 9.6.5
解决步骤
- 清理npm缓存:执行
npm cache clean --force - 删除本地依赖文件夹:Windows系统执行
rd /s /q node_modules,Linux/macOS执行rm -rf node_modules - 检查
package.json文件:确认所有依赖的版本号格式正确,无空值或无效版本定义 - 升级npm至最新稳定版:执行
npm install -g npm@latest - 重新执行
npm install或npm i --package-lock-only重建package-lock.json
内容的提问来源于stack exchange,提问作者ontherocks
相关产品推荐
相关产品推荐

