无法使用 typeScript 找到模块“mongoose”

Acu*_*vov 3 mongoose mongodb node.js typescript

我想在顶级函数中使用 wait,为此,typeScript 要求我将 tsconfig.json 中的目标语言设置为 2017 或更高版本,并将模块设置为 es2022。但是当我这样做时,出现以下错误:“找不到模块‘mongoose’。您的意思是将‘moduleResolution’选项设置为‘node’,还是将别名添加到‘paths’选项?”

我已经在很多地方进行了搜索,但没有找到解决方案。

这是我的一些代码:

tsconfig.json

{
"compilerOptions": {
 "target": "es2022",   
"module": "es2022",
"outDir": "./build",     
"esModuleInterop": true,   
 "forceConsistentCasingInFileNames": true,
 "strict": true,  
"skipLibCheck": true 
}
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "finanzas",
  "version": "1.0.0",
  "description": "API que permite gestionar las finanzas de un nucleo familiar o individuo",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node-dev src/index.ts",
    "start": "node build/index.js",
    "tsc": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Andres Felipe Cuervo",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/mongoose": "^5.11.97",
    "@types/node": "^17.0.44",
    "dotenv": "^16.0.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^4.7.3"
  },
  "dependencies": {
    "express": "^4.18.1",
    "mongoose": "^6.3.8"
  }
}
Run Code Online (Sandbox Code Playgroud)

当我像这样导入 mongoose 时会发生错误:

import mongoose from "mongoose";
Run Code Online (Sandbox Code Playgroud)

如果我用 require 导入它就没有错误

小智 6

维塔斯的回答对我来说也不起作用。

有效的方法是在我的 tsconfig 中取消注释以下内容:

"moduleResolution": "node" 
Run Code Online (Sandbox Code Playgroud)

希望能帮助其他遇到这个问题的人。