8

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
  • EventEmitter has no default exporter, try to import using import * as EventEmitter from 'eventemitter3' Commented May 14, 2017 at 14:15
  • @rpadovani I ran into this issue as well. With your code I couldn't extend the EventEmitter class which I think is strange.. Though this works: import {EventEmitter} from 'eventemitter3'. I guess it's really just a case by case solution. Thank you! Commented May 15, 2017 at 13:18

2 Answers 2

16

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

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

source

Sign up to request clarification or add additional context in comments.

1 Comment

The reason for this is the fact that for target: es6 and higher TypeScript by default uses module: ES6 and therefore moduleResolution: Classic: typescriptlang.org/docs/handbook/compiler-options.html And classic module resolution doesn't work with node_modules.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.