node.js - node_modules 文件中的 Typescript 错误:错误 TS2792:找不到模块 '../'
我刚开始学习 TypeScript。在编译 ts 项目时遇到了这个错误。
node_modules/@types/validator/lib/isBoolean.d.ts:1:23 - 错误 TS2792: 找不到模块 '../'。你是否想将 'moduleResolution' 选项设置为 'node',或者向 'paths' 选项添加别名?
1 import validator from '../';
~~~~~
在 ES5 中编译不会抛出任何错误,但在 ES6 及以上版本会报这个错误。
这是我的 tsconfig 文件,
{
"compilerOptions": {
"target": "ES2017",
"lib": ["DOM", "ES2017"],
"esModuleInterop": true,
"outDir": "dist",
"rootDir": "./src"
},
"include": ["./src/"],
"exclude": ["node_modules"],
}
这是我的 package.json,
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsc",
"tsc": "tsc --watch",
"start": "node dist/app.js",
"dev": "nodemon dist/app.js",
"deploy": "tsc && node dist/app.js"
},
"keywords": [],
"author": "",
"dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1",
"mongoose": "^6.5.2",
"postgres": "^3.2.4",
"reflect-metadata": "^0.1.13",
"sequelize-typescript": "^2.1.3",
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/sequelize": "^4.28.14",
"@types/validator": "^13.7.6",
"@tsconfig/node16": "^1.0.3",
"@types/express": "^4.17.13",
"@types/node": "^18.7.11",
"nodemon": "^2.0.19",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0"
}
}
答案#1
将 'moduleResolution' 选项设置为 'node' )
如果未设置选项“module”,“moduleResolution”=“node”
https://www.typescriptlang.org/docs/handbook/modules.html