【发布时间】:2019-02-25 16:46:01
【问题描述】:
似乎我的打字稿编译器在模块(./node_modules//index.d.ts)中时没有检测到模块的DefinedTypes文件,而是在@types文件夹(./node_modules/@types)中检测到它们//index.d.ts)。
例如使用 express-validator,该模块带有其定义的类型(请参阅:https://github.com/milkeg/testDefinedType/blob/master/node_modules/express-validator/index.d.ts)但是当我尝试使用 tsc 命令编译时,出现以下错误:
app.ts:5:28 - error TS2307: Cannot find module 'express-validator'.
5 import * as validator from 'express-validator';
我的 tsconfig.json 文件是:
{
"compilerOptions": {
"outDir": "./build",
"allowJs": true,
"target": "es2018",
"alwaysStrict": true,
},
"include": [
"./**/*",
],
"exclude": [
"./node_modules/**/*",
"./build/**/*"
],
}
我尝试将“./node_modules/**/*.d.ts”添加到包含选项,但没有帮助。
示例的回购:https://github.com/milkeg/testDefinedType
我应该进行哪些更改以确保 tsc 考虑到模块本身内的 .d.ts 文件(不仅在 ./node_modules/@types/* 下)?
【问题讨论】:
-
将“moduleResolution”更改为“node”是否是解决问题的好方法?
-
似乎将 "module": "commonjs" 添加到 tsconfig.json 解决了这个问题。我不清楚为什么。
-
如果您在
node_modules下有模块,您肯定希望将moduleResolution设置为node。设置"module": "commonjs"将默认moduleResolution更改为node;见the compiler options page。
标签: node.js typescript