TypeScript找不到带有index.d.ts的节点模块

Yov*_*var 4 node-modules typescript

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

import EventEmitter from 'eventemitter3'
Run Code Online (Sandbox Code Playgroud)

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

[ts] Cannot find module 'eventemitter3'.
Run Code Online (Sandbox Code Playgroud)

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

[ts] Cannot find module 'eventemitter3'.
Run Code Online (Sandbox Code Playgroud)

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

Yov*_*var 8

我通过在我的内容中添加以下内容解决了该问题tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "Node"
  }
}
Run Code Online (Sandbox Code Playgroud)

资源

  • 原因是这样的事实:默认情况下,对于“目标:es6”和更高版本的TypeScript使用“模块:ES6”,因此使用“模块分辨率:经典”:https://www.typescriptlang.org/docs/handbook/compiler-options .html并且经典模块解析不适用于`node_modules`。 (3认同)