在 Typescript 中使用 npm 模块 [英] Using npm modules in Typescript
问题描述
我想在 Typescript 项目中使用 npm 模块,但此模块没有类型或 tsd.当我尝试使用 import Module from 'module'
时出现错误:Cannot find module 'module'.
有没有办法修复它?
I would like to use npm module in Typescript project but there is no typings or tsd for this module. When I try use import Module from 'module'
I had an error : Cannot find module 'module'.
Is there way to fix it?
我的 tsconfig.json
:
{
"compilerOptions": {
"target": "ES5",
"moduleResolution": "node",
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"sourceMap": true,
"sourceRoot": "src",
"outDir": "bld"
},
"exclude": [
"bld"
]
}
推荐答案
我认为您的问题与导入模块有关
I assume your question is related to importing the module
import Module from 'module'
并且没有像您所说的那样导出它.如果是这种情况,您可以退回到普通的 javascript 并需要这样的模块:
And not exporting it as you stated. If this is the case your can fall back to plain javascript and require module like this:
var Module = require('module');
验证在 tsconfig.json 中的编译器选项中有以下几行:
Verify that in tsconfig.json you have the following lines in compiler options:
"compilerOptions": {
"moduleResolution": "node",
"module": "commonjs"
}
希望这会有所帮助.
这篇关于在 Typescript 中使用 npm 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!