如果已经安装了依赖项,则覆盖 package.json 中依赖项的 NPM 依赖项将不起作用

Roo*_*ooz 5 node.js npm typescript package.json package-lock.json

当我尝试使用 TypeScript 导入 mongoose 时,出现以下错误

node_modules/mongoose/node_modules/mongodb/mongodb.d.ts:3309:5 - error TS2416: Property 'end' in type 'GridFSBucketWriteStream' is not assignable to the same property in base type 'Writable'.
Run Code Online (Sandbox Code Playgroud)

显然这在 mongodb 4.3.0 中已修复,但 mongoose 使用 mongodb 4.2.2

我如何强制 mongoose 使用 mongodb 4.3.0+ ?

我尝试添加

  "overrides": {
    "mongoose": {
      "mongodb": "^4.3.0"
    }
  },
Run Code Online (Sandbox Code Playgroud)

到 package.json 但它没有解决问题。

这是我的 package.json

{
  "name": "reports",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "16.x"
  },
  "jest": {
    "setupFiles": [
      "<rootDir>/jestTestEnv.js"
    ]
  },
  "scripts": {
    "test": "jest --watchAll --setupFiles ./jestTestEnv.js",
    "dev": "tsc && nodemon -r dotenv/config dist/server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",

  "dependencies": {
    "body-parser": "^1.19.1",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "express-jwt": "^6.1.0",
    "express-openid-connect": "^2.5.2",
    "express-validator": "^6.14.0",
    "jwks-rsa": "^2.0.5",
    "mongoose": "^6.1.1"
  },
  "overrides": {
    "mongoose": {
      "mongodb": "^4.3.0"
    }
  },

  "devDependencies": {
    "@types/body-parser": "^1.19.2",
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/express-jwt": "^6.0.4",
    "@types/jest": "^27.4.0",
    "@types/node": "^17.0.13",
    "@types/supertest": "^2.0.11",
    "jest": "^27.4.5",
    "supertest": "^6.1.6",
    "typescript": "^4.5.5"
  }
}


Run Code Online (Sandbox Code Playgroud)

Roo*_*ooz 13

找到了解决方案。NPM 似乎有一个错误。删除 node_modules 和 package-lock.json 然后npm install修复问题。单独删除 node_modules 并不能解决问题。

  • 删除并重建 package-lock.json 将强制更新某些依赖项,除非它们已固定。为什么这是一个要求? (4认同)
  • 这有帮助!就我而言,它只是单独删除 `package-lock.json` 并不能修复它。删除两者都有帮助,谢谢! (2认同)