「报错解决」解决Vue2.6 npm i Could not resolve dependency问题

3,946 阅读6分钟

【前言】在 npm i 时遇到问题,以此文章进行记录 错误如下(已经省略了部分信息,这里只展示了主体信息),初步判断是找不到依赖包,

npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @0.1.0
npm ERR! Found: webpack@4.46.0
npm ERR! node_modules/webpack
npm ERR!   webpack@"^4.0.0" from @vue/cli-plugin-babel@3.12.1
npm ERR!   node_modules/@vue/cli-plugin-babel
npm ERR!     dev @vue/cli-plugin-babel@"^3.6.0" from the root project
npm ERR!   webpack@"^4.0.0" from @vue/cli-service@3.12.1
npm ERR!   node_modules/@vue/cli-service
npm ERR!     dev @vue/cli-service@"^3.6.0" from the root project
npm ERR!     @vue/cli-service@"^3.0.0-rc.3" from vue-cli-plugin-browser-sync@1.0.0-rc5
npm ERR!     node_modules/vue-cli-plugin-browser-sync
npm ERR!       dev vue-cli-plugin-browser-sync@"^1.0.0-rc5" from the root project
npm ERR!   5 more (babel-loader, file-loader, less-loader, sass-loader, vue-cli-plugin-browser-sync)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! dev vue-loader@"14.2.3" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: webpack@5.70.0
npm ERR! node_modules/webpack
npm ERR!   peer webpack@"^5.0.0" from css-loader@6.7.1
npm ERR!   node_modules/css-loader
npm ERR!     peer css-loader@"*" from vue-loader@14.2.3
npm ERR!     node_modules/vue-loader
npm ERR!       dev vue-loader@"14.2.3" 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! 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 i --force

npm WARN ERESOLVE overriding peer dependency
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'pkcs7@0.2.3',
npm WARN EBADENGINE   required: { npm: '^1.4.6', node: '^0.10' },
npm WARN EBADENGINE   current: { node: 'v14.17.5', npm: '8.1.4' }
npm WARN EBADENGINE }
npm WARN deprecated natives@1.1.6: This module relies on Node.js's internals and will break at some point. Do not use it, and update to graceful-fs@4.x.
npm WARN deprecated flatten@1.0.3: flatten is deprecated in favor of utility frameworks such as lodash.
npm WARN deprecated tsml@1.0.1: no longer maintained
npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated browserslist@1.7.7: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated cryptiles@2.0.5: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated source-map-url@0.4.0: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated boom@2.10.1: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated html-webpack-plugin@3.2.0: 3.x is no longer supported
npm WARN deprecated sntp@1.0.9: This module moved to @hapi/sntp. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated vue-qriously@1.1.1: This package is no longer maintained
npm WARN deprecated @xkeshi/vue-barcode@1.0.0: No longer maintainted, please use @chenfengyuan/vue-barcode
npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address'
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated hoek@2.16.3: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated request@2.81.0: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@4.2.1: this library is no longer supported
npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated hawk@3.1.3: This module moved to @hapi/hawk. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi'
npm WARN deprecated svgo@0.7.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.
npm WARN deprecated core-js@2.6.12: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.

added 1843 packages in 1m

好像到这里就ok了,No!远远没有想象的这么简单,结果就是又又又报错了Syntax Error: TypeError: this.getOptions is not a function

image.png 筛查了一遍这个相关的组件后发现,组件暂无问题,查阅网上资料发现是某些依赖的版本问题,只能慢慢进行筛查,首先由于项目内使用的是sass,第一个就去筛查了sass-loader,node-sass,版本对照之后进行了不停安装各种版本,还是不行,一直提示Syntax Error: TypeError: this.getOptions is not a function

于是我决定重新理清楚思路,删除node_moudles进行重装。 根据提示this command with --force, or --legacy-peer-deps,我查阅了许多大佬对于这两个命令的解读,大概了解了这两个命令的基本作用

  • npm i --forece 作用:会无视冲突,并强制获取远端npm库资源(即使本地有资源也会覆盖)。
  • npm i --legacy-peer-deps作用:安装时忽略所有peerDependencies(对等依赖),忽视依赖冲突,采用npm版本4到版本6的样式去安装依赖,已有的依赖不会覆盖。

总而言之,采用第二种方式会相对保险一些

于是执行第二个命令npm i --legacy-peer-deps 结果如下

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'pkcs7@0.2.3',
npm WARN EBADENGINE   required: { npm: '^1.4.6', node: '^0.10' },
npm WARN EBADENGINE   current: { node: 'v14.17.5', npm: '8.1.4' }
npm WARN EBADENGINE }
npm WARN deprecated natives@1.1.6: This module relies on Node.js's internals and will break at some point. Do not use it, and update to graceful-fs@4.x.
npm WARN deprecated flatten@1.0.3: flatten is deprecated in favor of utility frameworks such as lodash.
npm WARN deprecated tsml@1.0.1: no longer maintained
npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142      
npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated browserslist@1.7.7: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated cryptiles@2.0.5: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated source-map-url@0.4.0: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated boom@2.10.1: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated html-webpack-plugin@3.2.0: 3.x is no longer supported
npm WARN deprecated sntp@1.0.9: This module moved to @hapi/sntp. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated vue-qriously@1.1.1: This package is no longer maintained
npm WARN deprecated @xkeshi/vue-barcode@1.0.0: No longer maintainted, please use @chenfengyuan/vue-barcode
npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address'
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated hoek@2.16.3: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated request@2.81.0: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@4.2.1: this library is no longer supported
npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated hawk@3.1.3: This module moved to @hapi/hawk. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi'
npm WARN deprecated svgo@0.7.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.
npm WARN deprecated core-js@2.6.12: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.

added 1858 packages in 2m

原以为还是有问题,执行一下npm run serve后,发现还是报错,但是!这次的报错显然不太相同!报错如下(路径省略)

Syntax Error: Error: ENOENT: no such file or directory, scandir 'D:\...\node_modules\node-sass\vendor'

这个报错指的是没有找到相对于的文件依赖,需要重新构建一下,执行构建时会发生错误,这时候马上执行npm rebuild node-sass重构一下node-sass即可,再执行一遍npm run serve,你就会惊喜的发现Compiled successfully in 25957ms

写在最后,在这个过程中还有许多不理解,但是也算是解决了问题吧,希望以后能多学习学习再学习!!冲冲冲