[英]Cannot find module 'mongoose' with typeScript
我想在頂級 function 中使用 await,為此 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
}
}
package.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"
}
}
當我像這樣導入 mongoose 時發生錯誤:
import mongoose from "mongoose";
如果我用 require 導入它沒有錯誤
這個問題的解決方案是在你的項目基目錄中找到的 tsconfig.json 文件中注釋下面的行,只需注釋這一行。
"esModuleInterop":true
還deprecated @types/mongoose@5.11.97: Mongoose publishes its own types, so you do not need to install this package.
Vitals 的回答對我也沒有用。
起作用的是在我的 tsconfig 中取消注釋以下內容:
"moduleResolution": "node"
希望能幫助其他遇到此問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.