简体   繁体   English

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

[英]TypeScript cannot find node module with index.d.ts

I'm trying to use EventEmmiter3 with the following syntax:我正在尝试使用以下语法使用EventEmmiter3

import EventEmitter from 'eventemitter3'

I have this module installed under the ./node_modules folder.我在./node_modules文件夹下安装了这个模块。 This module contains a index.d.ts so I think it should be detected by Typescript.这个模块包含一个index.d.ts所以我认为它应该被 Typescript 检测到。 But instead get the error:但反而得到错误:

[ts] Cannot find module 'eventemitter3'.

I tried adding ./node_modules to the included types in my tsconfig.json without success:我尝试将./node_modules添加到我的tsconfig.json中包含的类型,但没有成功:

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

How should I configure Typescript to find node modules?我应该如何配置 Typescript 来查找节点模块?

I solved it by adding the following in my tsconfig.json : 我通过在tsconfig.json添加以下内容来解决它:

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

source 资源

For ECMAScript that use import statement, set the moduleResolution to 'nodenext' instead对于使用import语句的 ECMAScript,请将moduleResolution设置为 'nodenext'

{
  "compilerOptions": {
    "moduleResolution": "nodenext"   // or 'node16'
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM