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

问题描述 投票:0回答:2

当我尝试使用 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'.

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

如何强制 mongoose 使用 mongodb 4.3.0+ ?

我尝试添加

  "overrides": {
    "mongoose": {
      "mongodb": "^4.3.0"
    }
  },

到 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"
  }
}


node.js typescript npm package.json package-lock.json
2个回答
13
投票

找到解决方案。 NPM 似乎有一个错误。删除 node_modules 和 package-lock.json 然后执行

npm install
修复了该问题。单独删除 node_modules 并不能解决问题。


0
投票

我在 npm 10.2.5 上仍然遇到 这个错误

我的解决方法是在运行 package-lock.json 之前,从

npm install
手动删除有问题的包的条目

不建议删除整个package-lock.json。删除 node_modules 也不是必要的。

© www.soinside.com 2019 - 2024. All rights reserved.