简体   繁体   中英

TypeScript cannot find node module with index.d.ts

I'm trying to use EventEmmiter3 with the following syntax:

import EventEmitter from 'eventemitter3'

I have this module installed under the ./node_modules folder. This module contains a index.d.ts so I think it should be detected by 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:

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

How should I configure Typescript to find node modules?

I solved it by adding the following in my tsconfig.json :

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

source

For ECMAScript that use import statement, set the moduleResolution to 'nodenext' instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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