ZakiMa
ZakiMa

Reputation: 6271

How to block typescript 2.0 from walking up all parent directories while resolving modules?

Upgraded to Typescript 2.0 (2.1.6) and it started giving "Duplicate identifier" errors. After taking a closer look it turned out that Typescript started importing @types from all upper directories (essentially other projects).

What should be the configuration to let Typescript to ignore upper node_modules?

src
 └── node_modules << *** how to ignore it? ***
     └── @types

 └── my.app << *** how to build this folder and down only? ***
         └── node_modules
             └── @types

EDIT: Here is an example of error I'm getting:

typings/globals/mocha/index.d.ts(30,13): error TS2300: Duplicate identifier 'describe'. ../../../node_modules/@types/jasmine/index.d.ts(9,18): error TS2300: Duplicate identifier 'describe'.

listFiles: true shows @types/jasmine being imported from upper folder:

C:/src/<project>/<folder>/<my.app>/typings/globals/mocha/index.d.ts
C:/src/node_modules/@types/jasmine/index.d.ts

If I rename upper node_modules folder then build succeeds.

Upvotes: 27

Views: 6503

Answers (5)

benjick
benjick

Reputation: 149

You need both baseUrl and typeRoots for it to actually work.

"compilerOptions": {
  "baseUrl": ".",
  "typeRoots": ["./node_modules/@types"]
},

Upvotes: 2

devinm
devinm

Reputation: 865

In this helps anyone, setting "types": [] in my tsconfig.json worked for me. See this github comment.

https://github.com/Microsoft/TypeScript/issues/13992#issuecomment-279020210

Upvotes: 4

Jesse
Jesse

Reputation: 10466

I had this issue, and inferred the same thing as @peterjwest from the docs - however after reading this issue: https://github.com/Microsoft/TypeScript/issues/27026, I think I misunderstood the intent of typeRoots (which appears to be useful only when configuring global types rather than module types).

In my case, the solution was to configure baseUrl and paths for the conflicting type (which in my case was react):

tsconfig.json:

{
    "compilerOptions": {
        ...
        "baseUrl": ".",
        "paths": {
            "react": ["node_modules/@types/react"]
        }
    }
    ...
}

This seems to work for telling typescript that I want to specifically resolve that type from my local node_modules.

Upvotes: 1

peterjwest
peterjwest

Reputation: 4452

The official documentation specifies that node_modules in current directory and all parents will be traversed unless you specify typeRoots.

So in theory, the answer should be this:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

Since you still want to include the types from the current directory.

Unfortunately this doesn't seem to work correctly for me.

Upvotes: 28

Sebastian Sebald
Sebastian Sebald

Reputation: 16856

You can specify a root directory in the compiler options. See the official documentation.

{
   "compilerOptions": {
       "typeRoots" : ["./typings"]
   }
}

Upvotes: 0

Related Questions