node和webpack版本冲突导致无法安装依赖

can't install dependencies because of node and webpack version conflict

我正在尝试使用 json 文件中列出的确切版本安装依赖项,但出现以下错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! Found: webpack@4.46.0
npm ERR! node_modules/webpack
npm ERR!   webpack@"^4.35.3" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@"^5.1.0" from terser-webpack-plugin@5.3.1
npm ERR! node_modules/terser-webpack-plugin
npm ERR!   dev terser-webpack-plugin@"^5.1.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.

由于所需版本的依赖项不是最新的,我将我的 node.js 从最新版本 16 降级到 14+,并使用 npm i --leacy-peer-deps 安装,但是错误显示仍然相同,似乎节点或 webpack 的版本仍然与依赖项不兼容(找到:webpack@4.46.0 错误! node_modules/webpack 错误! webpack@"^4.35.3" 来自根项目).

我该如何解决这个问题?

这是 json 列表:

{
  "name": "example-project",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/server/index.js",
    "build-prod": "webpack --config webpack.prod.js",
    "build-dev": "webpack-dev-server  --config webpack.dev.js --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jest-fetch-mock": "^3.0.3",
    "webpack": "^4.35.3",
    "webpack-cli": "^3.3.5"
  },
  "devDependencies": {
    "@babel/core": "^7.13.15",
    "@babel/plugin-transform-modules-commonjs": "^7.13.8",
    "@babel/preset-env": "^7.13.15",
    "babel-loader": "^8.2.2",
    "body-parser": "^1.19.0",
    "clean-webpack-plugin": "^3.0.0",
    "cors": "^2.8.5",
    "css-loader": "^5.2.1",
    "html-webpack-plugin": "^3.2.0",
    "jest": "^26.6.3",
    "mini-css-extract-plugin": "^1.4.1",
    "node-fetch": "^2.6.1",
    "node-sass": "^5.0.0",
    "optimize-css-assets-webpack-plugin": "^5.0.4",
    "sass": "^1.32.8",
    "sass-loader": "^10.1.1",
    "style-loader": "^2.0.0",
    "terser-webpack-plugin": "^5.1.1",
    "webpack-dev-server": "^3.11.2",
    "workbox-webpack-plugin": "^6.1.5"
  }
}

尝试删除 package-lock.json 并在 npm i.

之前使用 'npm cache clean -f' 清除 npm 缓存

插入符 (^) 表示法:它将更新到所有未来的 minor/patch 版本,而不增加主要版本。 ^2.3.4 将使用从 2.3.4 到 <3.0.0

的版本

波浪号 (~):它将更新到所有未来的补丁版本,而不会增加次要版本。 ~1.2.3 将使用从 1.2.3 到 <1.3 的版本。