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?
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.
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.
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).
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' .
I solved it by adding the following in my tsconfig.json
:
{
"compilerOptions": {
"moduleResolution": "Node"
}
}
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With