在Typescript中使用npm模块

qwe*_*asd 9 npm typescript

我想在Typescript项目中使用npm模块,但这个模块没有任何打字或tsd.当我尝试使用时,import Module from 'module'我遇到了一个错误:Cannot find module 'module'.有没有办法解决它?

[编辑]我的tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "moduleResolution": "node",
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "sourceRoot": "src",
    "outDir": "bld"
  },
  "exclude": [
    "bld"
  ]
}
Run Code Online (Sandbox Code Playgroud)

Ami*_*mid 11

我假设您的问题与导入模块有关

import Module from 'module'
Run Code Online (Sandbox Code Playgroud)

而不是像你说的那样出口它.如果是这种情况,你可以回到普通的javascript并需要这样的模块:

var Module = require('module');
Run Code Online (Sandbox Code Playgroud)

[编辑]

验证在tsconfig.json中,您在编译器选项中有以下行:

"compilerOptions": { 
    "moduleResolution": "node",
    "module": "commonjs"
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.