Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 791
Yovar Avatar asked May 14 '17 13:05

Yovar


People also ask

Can not find module TS?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

Where does node look for Node_modules?

Node will look for your modules in special folders named node_modules . A node_modules folder can be on the same level as the current file, or higher up in the directory chain. Node will walk up the directory chain, looking through each node_modules until it finds the module you tried to load.

What is declare module in TypeScript?

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

Can not find module path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .


1 Answers

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

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

source

like image 113
Yovar Avatar answered Oct 14 '22 07:10

Yovar