【问题标题】:TypeScript cannot find node module with index.d.tsTypeScript 找不到带有 index.d.ts 的节点模块
【发布时间】:2017-10-13 08:27:41
【问题描述】:

我正在尝试通过以下语法使用EventEmmiter3

import EventEmitter from 'eventemitter3'

我在./node_modules 文件夹下安装了这个模块。这个模块包含一个index.d.ts,所以我认为它应该被 Typescript 检测到。但反而得到错误:

[ts] Cannot find module 'eventemitter3'.

我尝试将./node_modules 添加到我的tsconfig.json 中包含的类型中,但没有成功:

{
  "compilerOptions": {
    "typeRoots": ["./node_modules", "./node_modules/@types"]
  }
}

我应该如何配置 Typescript 来查找节点模块?

【问题讨论】:

  • EventEmitter 没有默认导出器,尝试使用import * as EventEmitter from 'eventemitter3'导入
  • @rpadovani 我也遇到了这个问题。使用您的代码,我无法扩展我认为很奇怪的 EventEmitter 类。虽然这有效:import {EventEmitter} from 'eventemitter3'。我想这真的只是个案解决方案。谢谢!

标签: typescript node-modules


【解决方案1】:

我通过在tsconfig.json 中添加以下内容解决了这个问题:

{
  "compilerOptions": {
    "moduleResolution": "Node"
  }
}

source

【讨论】:

猜你喜欢
  • 2016-11-10
  • 2021-12-07
  • 1970-01-01
  • 2022-12-15
  • 2017-07-04
  • 2017-03-19
  • 2021-01-06
  • 1970-01-01
  • 2017-09-17
相关资源
最近更新 更多