Typescript: Unable to find type definitions that are provided by the package rather than @types when not using commonjs

Created on 5 Mar 2017  ·  2Comments  ·  Source: microsoft/TypeScript

I'm trying to compile in ES2015 but the compiler fails to find Redux's type definitions. The definitions are located in the Redux package itself (not @types). This works with module: "commonjs" compiler option but not with any other module system.

I can't import it with a triple-slash reference (/// <reference path="node_modules/redux/index.d.ts" />) neither because the file doesn't have a declare module statement. And doing it with types (/// <reference types="redux" />) will fail because there is no file in @types/redux.

That being said I can work around it by copying the node_modules/redux/index.d.ts file to node_modules/@types/redux/index.d.ts or by wrapping the whole type definition file into a declare module "redux".

I also tried to fiddle with the typeRoots and types compiler options. The best that came out of it is adding node_modules to typeRoots to it successfully locates the Redux package with its index.d.ts however I will get an error for each of every other directory. Something like this:

error TS2688: Cannot find type definition file for '.bin'.
error TS2688: Cannot find type definition file for '@types'.
error TS2688: Cannot find type definition file for 'js-tokens'.
error TS2688: Cannot find type definition file for 'lodash'.
error TS2688: Cannot find type definition file for 'lodash-es'.
error TS2688: Cannot find type definition file for 'loose-envify'.

TypeScript Version: 2.1.1 (Windows)

Code

import { createStore } from "redux";

tsconfig.json

{
    "compilerOptions": {
        "module": "es2015"
    }
}

Expected behavior:
Compiles succesfully
Actual behavior:
Error thrown error TS2307: Cannot find module 'redux'.

Most helpful comment

Try changing moduleResolution option to node value. It defaults to node with commonjs modules.

Documentation of module resolution.

All 2 comments

Try changing moduleResolution option to node value. It defaults to node with commonjs modules.

Documentation of module resolution.

It worked! I didn't think about it at all :pensive: . This explains why it worked only with commonjs.

Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings