我在新电脑下载完项目代码后,运行
npm install --registry=https://registry.npm.taobao.org
1运行就报错:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: lowcode-form-backend@1.0.0
npm ERR! Found: mongoose@5.13.22
npm ERR! node_modules/mongoose
npm ERR! dev mongoose@"^5.7.5" 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! dev 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.
npm ERR!
npm ERR! See C:\Users\13066\AppData\Local\npm-cache\eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\13066\AppData\Local\npm-cache_logs\2024-01-11T08_07_58_049Z-debug.log
D:\h5\code\gitlab\backend\lowcode-form-backend>npx mongoose --version
npm ERR! could not determine executable to run
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\13066\AppData\Local\npm-cache_logs\2024-01-11T08_13_20_531Z-debug.log
分析问题:看到这个错误提示,就发现是mongoose这个数据库的版本冲突了,那我就想搞明白,这些是谁?
于是我通过cmd去输入mongoose --version,发现没找到相应命令,那就不是电脑的mongoose版本,于是通过visiable studio 查看,发现了端倪:
从上图发现有3个文件有这个,对于我来讲,只要package.json,那我就把剩下的两个删除即可,删除后运行结果如下:
发现解决了不了问题:
于是我去mongoose-auto-increment - npm查找了下mongoose-auto-increment,发现它最新的一版竟然是8年前?
8年前是2016年,那2016年mongoose是什么版本?
我查了一下:
我大概我理解了意思:
怎么办?
那就把mongoose改成大家可以接受的版本:
"mongoose": "^4.1.12",
删除package-lock.json和node_modules,重新运行:npm install --registry=https://registry.npm.taobao.org
可以安装成功
运行npm run dev跑起来,报如下错误:
throw new TypeError('Undefined type ' + name + ' at `' + path +
^
TypeError: Undefined type ObjectID at _id
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (D:\h5\code\gitlab\backend\lowcode-form-backend\node_modules\mongoose\lib\schema.js:673:11)
at Schema.path (D:\h5\code\gitlab\backend\lowcode-form-backend\node_modules\mongoose\lib\schema.js:529:29)
at Schema.add (D:\h5\code\gitlab\backend\lowcode-form-backend\node_modules\mongoose\lib\schema.js:411:12)
at new Schema (D:\h5\code\gitlab\backend\lowcode-form-backend\node_modules\mongoose\lib\schema.js:105:10)
at Object. (D:\h5\code\gitlab\backend\lowcode-form-backend\models\user.js:8:21)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (D:\h5\code\gitlab\backend\lowcode-form-backend\routes\auth.js:3:15)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
怎么办?看上面的错误看样子是ObjectId 没有,会不会是版本不对?
我去查看下,相应的版本是否正确
node_modules\mongoose-auto-increment\package.json "version": "5.0.1",版本正常:
\node_modules\mongoose\package.json "version": "4.13.21",
好像不太对,mongoose-auto-increment要求的是4.1.12, 实际是4.13.21,
配置代码:"mongoose": "^4.1.12",表示可以4.x.x,但不包含5.x.x
为了让运行不报错误,于是我改成"mongoose": "4.1.12",
删除mongoose重新运行
npm install mongoose --registry=https://registry.npm.taobao.org
结果一样,崩溃了,怎么办?
找帮哥,结果他给了我一份他那里的下载好的node_modules
我直接放到目录上,运行npm run dev
成功!
我错在哪?
我看了node_modules\mongoose-auto-increment\package.json的依赖,这句表示"mongoose": "^4.1.12",mongoose只能是4.x.x了,如果是5.x.x和6.x.x是不行的
"dependencies": {
"extend": "^3.0.0"
},
"peerDependencies": {
"mongoose": "^4.1.12"
},
"devDependencies": {
"async": "","chai": "",
"mocha": "*",
"mongoose": "^4.1.12"
}
但我看了同事给我的,他的node_modules\mongoose\package.json "version": "5.13.21",就是5.xx.xx,不是4.xx.xx,从上面的错误看出来还有一个--force参数,可以强制成某个版本如下图
于是我想到了另外一个办法,就是使用npm install --force --registry=https://registry.npm.taobao.org
删除node_modules重新运行npm install --force --registry=https://registry.npm.taobao.org
结果刚刚的报错竟然变成警告,可以继续往下走:
下载完成后,我去查看node_modules\mongoose\package.json "version": "5.13.22" 竟然如此的像,好有了,有戏了,运行npm run dev试试
结果运行成功,如下图:
我在想npm install --legacy-peer-deps --registry=https://registry.npm.taobao.org,可以么?
我试了下,还真可以,
运行结果:我去查看node_modules\mongoose\package.json "version": "5.13.22",和上面一样,运行npm run dev也行
总结:
面对上面的冲突,我们有三种解决:
①如果有同事能成功了,直接复制他的node_modules也行,因为是下载环节出问题了,现在这种办法就是跳过下载环节
②使用 --force或者 --legacy-peer-deps 让npm去兼容这种场景
③手动调整被依赖的冲突包的版本,以达到几个依赖都能兼容,再测试运行看效果