完全相同的 package.json 文件在空的全新项目中有效,但在旧项目中无效

Same exact package.json file works in empty brand new project but doesn't in old project

当我尝试升级到 Angular 8.

时出现此错误

npm WARN old lockfile

npm WARN old lockfile The package-lock.json file was created with an old version of npm,

npm WARN old lockfile so supplemental metadata must be fetched from the registry.

npm WARN old lockfile

npm WARN old lockfile This is a one-time fix-up, please be patient...

npm WARN old lockfile

npm ERR! code ERESOLVE

npm ERR! ERESOLVE could not resolve

npm ERR!

npm ERR! While resolving: comprehensivedashboard@0.0.0

npm ERR! Found: @angular/animations@7.2.6

npm ERR! node_modules/@angular/animations

npm ERR! @angular/animations@"~8.2.14" from the root project

npm ERR! peer @angular/animations@">=7.0.0" from @angular/material@7.3.3

npm ERR! node_modules/@angular/material

npm ERR! @angular/material@"~8.2.3" from the root project

npm ERR!

npm ERR! Could not resolve dependency:

npm ERR! @angular/animations@"~8.2.14" from the root project

npm ERR!

npm ERR! Conflicting peer dependency: @angular/core@8.2.14

npm ERR! node_modules/@angular/core

npm ERR! peer @angular/core@"8.2.14" from @angular/animations@8.2.14

npm ERR! node_modules/@angular/animations

npm ERR! @angular/animations@"~8.2.14" 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\vakkinen\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\vakkinen\AppData\Local\npm-cache_logs21-10-05T12_19_50_257Z-debug.log

因此决定创建一个全新的 Angular8 应用程序并查看 package.json 文件的外观。该文件附在下面。然后我将我需要的所有包一次一个地添加到这个全新的空项目中。应用程序构建成功,我能够 运行 应用程序。因此,我将所有依赖项和 devDependencies 复制并粘贴到旧项目 package.json。由于上述相同的错误,该项目仍然失败 npm i。所以我尝试将 repo 克隆到一个新目录中,并将 package.json 的内容替换为全新空项目中的 package.json 的内容,仍然是同样的错误。我做错了什么?

{
  "name": "dashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.2.14",
    "@angular/cdk": "8.2.3",
    "@angular/common": "~8.2.14",
    "@angular/compiler": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/material": "~8.2.3",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "@ng-bootstrap/ng-bootstrap": "^5.3.0",
    "bootstrap": "^4.3.1",
    "core-js": "^2.5.4",
    "@swimlane/ngx-charts": "^11.0.0",
    "fusioncharts": "^3.15.0-sr.1",
    "hammerjs": "^2.0.8",
    "moment": "^2.24.0",
    "ngx-bootstrap": "^3.2.0",
    "ngx-csv": "^0.3.1",
    "ngx-export-as": "1.4.2",
    "ngx-scrollbar": "^4.1.1",
    "ngx-select-dropdown": "^1.0.1",
    "node-sass": "^4.14.0",
    "rxjs": "~6.4.0",
    "sass-loader": "^8.0.2",
    "tslib": "^1.10.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",    
    "@angular/cli": "~8.3.29",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.7.0",
    "protractor": "~7.0.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.5.3"
  }
}

在与包文件相同的文件夹中应该有一个名为 package-lock 的文件。删除它,错误应该消失。 package-lock 文件包含您的依赖项及其版本的所有依赖项。删除它不会影响你的项目,因为它是在 npm install 上重新生成的。

错误消息警告您 package-lock.json 文件是问题所在。直接删掉,又会生成一个新的

问题源于 node_modules 目录和 package-lock.json.

目录中的文件不正确

很可能最简单的解决方案是同时删除 node_modules 目录和包-lock.json 然后 运行 一个 npm install 得到一个干净的 node_modules 目录和一个新的 package-lock.json 根据你的 package.json

我建议阅读更多关于 package-lock.json 文件的用途:https://medium.com/coinmonks/everything-you-wanted-to-know-about-package-lock-json-b81911aa8ab8

解决此问题的步骤:

  1. 删除 node_modules 文件夹。
  2. 删除package-lock.json.
  3. 运行 npm i --force.