TypeScript cannot find node module with index.d.ts

Viewed 9894

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?

2 Answers

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

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