在 Typescript 中使用 npm 模块 [英] Using npm modules in Typescript

查看:45
本文介绍了在 Typescript 中使用 npm 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆