Express项目安装node_modules遇依赖冲突与编译错误,求解决方案
问题与解决方案
问题1:依赖树解析失败(ERESOLVE错误)
报错信息
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: nfttoken@1.0.0 npm ERR! Found: mongoose@5.13.15 npm ERR! node_modules/mongoose npm ERR! mongoose@"^5.13.7" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer mongoose@"^4.1.12" from mongoose-auto-increment@5.0.1 npm ERR! node_modules/mongoose-auto-increment npm ERR! mongoose-auto-increment@"^5.0.1" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
项目依赖(package.json)
"dependencies": { "express": "^4.17.1", "express-fileupload": "^1.2.1", "express-validator": "^6.12.1", "mongoose": "^5.13.7", "mongoose-auto-increment": "^5.0.1", "mongoose-sequence": "^5.3.1", "xml-formatter": "^2.6.1", "xml2json": "^0.12.0" }
问题2:node-expat编译失败(缺少make工具)
再次执行npm install后报错:
npm ERR! code 1 npm ERR! path <PROJECT-PATH>backend/node_modules/node-expat npm ERR! command failed npm ERR! command sh /tmp/install-1e860795.sh npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.0.0 npm ERR! gyp info using node@16.17.0 | linux | x64 npm ERR! gyp info find Python using Python version 3.8.10 found at "/usr/bin/python3" npm ERR! gyp info spawn /usr/bin/python3 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/usr/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '<PROJECT-PATH>backend/node_modules/node-expat/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/usr/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '<HOME-PATH>.cache/node-gyp/16.17.0/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=<HOME-PATH>.cache/node-gyp/16.17.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/usr/lib/node_modules/npm/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=<HOME-PATH>.cache/node-gyp/16.17.0/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=<PROJECT-PATH>backend/node_modules/node-expat', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: not found: make npm ERR! gyp ERR! stack at getNotFoundError (/usr/lib/node_modules/npm/node_modules/which/which.js:10:17) npm ERR! gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/which/which.js:57:18 npm ERR! gyp ERR! stack at new Promise (<anonymous>) npm ERR! gyp ERR! stack at step (/usr/lib/node_modules/npm/node_modules/which/which.js:54:21) npm ERR! gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/which/which.js:71:22 npm ERR! gyp ERR! stack at new Promise (<anonymous>) npm ERR! gyp ERR! stack at subStep (/usr/lib/node_modules/npm/node_modules/which/which.js:69:33) npm ERR! gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/which/which.js:80:22 npm ERR! gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/index.js:42:5 npm ERR! gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/mode.js:8:5 npm ERR! gyp ERR! System Linux 5.15.0-46-generic npm ERR! gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd <PROJECT-PATH>backend/node_modules/node-expat npm ERR! gyp ERR! node -v v16.17.0 npm ERR! gyp ERR! node-gyp -v v9.0.0 npm ERR! gyp ERR! not ok
解决方案
针对问题1:依赖冲突解决
核心矛盾是mongoose-auto-increment@5.0.1仅支持mongoose 4.x,而你使用的是mongoose 5.x,可选以下方案:
替换依赖(推荐):
你已安装mongoose-sequence,它是mongoose-auto-increment的替代方案,完美支持mongoose 5.x+。直接移除旧依赖并重新安装:npm uninstall mongoose-auto-increment npm install后续修改代码中的自动递增逻辑,改用
mongoose-sequence即可,其用法更简洁稳定。忽略peer依赖检查:
若暂时不想修改代码,可执行安装命令时跳过版本冲突校验:npm install --legacy-peer-deps该参数会让npm采用v6版本的peer依赖处理逻辑,强制安装,但可能存在潜在兼容性风险,不建议长期使用。
降级mongoose版本:
将package.json中mongoose版本改为^4.1.12,再重新安装:npm install mongoose@^4.1.12此方法会导致mongoose版本过旧,可能引入安全和功能缺失问题,不推荐。
针对问题2:缺少编译工具(make)
报错明确提示系统未安装make,需安装Linux编译依赖包:
对于Debian/Ubuntu系系统,执行:
sudo apt update && sudo apt install build-essential
该包会包含make、gcc、g++等编译所需的核心工具,安装完成后重新执行npm install即可完成node-expat的编译安装。
内容的提问来源于stack exchange,提问作者Kiddo
相关产品推荐
相关产品推荐

