typescript - 在 Typescript 中使用 npm 模块

标签 typescript npm

我想在 Typescript 项目中使用 npm 模块,但是这个模块没有 typings 或 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"
  ]
}

最佳答案

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

import Module from 'module'

并没有像您所说的那样导出它。如果是这种情况,您可以退回到普通的 javascript 并像这样需要模块:

var Module = require('module');

[编辑]

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

"compilerOptions": { 
    "moduleResolution": "node",
    "module": "commonjs"
}

希望这对您有所帮助。

关于typescript - 在 Typescript 中使用 npm 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36309361/

相关文章:

angularjs - 无需要求即可在 typescript 中导入模板

Typescript:无效的 Object.assign 类型?

angular - 如何创建一个准备发布到 npm angular 2 库

npm - 如何使用 npm run 脚本运行 'imagemin' 插件?

module - typescript :导入类的 'Cannot find name'错误

node.js - 模型相互引用错误 : circular dependencies problem

typescript 联合类型到单一映射类型

javascript - 从终端运行 npm 时为 "module.js:557 throw err"

javascript - compilation.emitAsset 不是 sitemap-webpack-plugin 中的函数

node.js - 模块应该在哪里写入文件?