271

Why am I getting this and many more errors of this kind? I am adding a link to the repo as well as key code snippets below. I think I have a basic misunderstanding of how the dependency and "include" chaining works.

csvproc(master)> tsc
node_modules/typescript/bin/lib.core.d.ts(83,5): error TS2300: Duplicate identifier 'configurable'.
node_modules/typescript/bin/lib.core.d.ts(84,5): error TS2300: Duplicate identifier 'enumerable'.
node_modules/typescript/bin/lib.core.d.ts(85,5): error TS2300: Duplicate identifier 'value'.
node_modules/typescript/bin/lib.core.d.ts(86,5): error TS2300: Duplicate identifier 'writable'.

All code can be found here.

My tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": false,
        "outDir": "built/",
        "sourceMap": true,
        "target": "es5"
    }
}

My tsd.json:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "node/node-0.10.d.ts": {
      "commit": "6387999eb899d0ba02d37dd8697647718caca230"
    },
    "should/should.d.ts": {
      "commit": "e1182d56ccb192379eade6055d9ba3fb6a0bacc4"
    }
  }
}

My tsd.d.ts:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "node/node-0.10.d.ts": {
      "commit": "6387999eb899d0ba02d37dd8697647718caca230"
    },
    "should/should.d.ts": {
      "commit": "e1182d56ccb192379eade6055d9ba3fb6a0bacc4"
    }
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
pitosalas
  • 10,286
  • 12
  • 72
  • 120

21 Answers21

145

This is because of the combination of two things:

  • tsconfig not having any files section. From http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

    If no "files" property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories. When a "files" property is specified, only those files are included.

  • Including typescript as an npm dependency : node_modules/typescript/ This means that all of typescript gets included .... there is an implicitly included lib.d.ts in your project anyways (http://basarat.gitbook.io/typescript/content/docs/types/lib.d.ts.html) and its conflicting with the one that ships with the NPM version of typescript.

Fix

Either list files or include explicitly https://basarat.gitbook.io/typescript/project/compilation-context/files

Liran H
  • 9,143
  • 7
  • 39
  • 52
basarat
  • 261,912
  • 58
  • 460
  • 511
  • 9
    Or install typescript globally instead? – Ryan Cavanaugh Jul 10 '15 at 00:10
  • 2
    Yeah I don't know why its there locally :) I assumed perhaps he needed some api from it (in hindsight ... its unlikely) – basarat Jul 10 '15 at 00:13
  • RyanCavanaugh yes, that was the problem; and @basarat, I'm not sophisticated yet, so it was just an oversight that I had it both locally and globally. Or in my defense, that doing that in the ruby world where I come from doesn't harm things, so that was my context :) Still the info you added to your response continues to complete the picture for me. So thanks. both of you! – pitosalas Jul 10 '15 at 00:58
  • Complete Gulp newbie here, but I've removed typescript from the node_modules folder, and installed it globally, but now the gulp task complains that it can't find the typescript module, and it's looking for it in the folder I've just removed. I'm guessing I've missed something, but I can't work out what. – Paul Manzotti Nov 22 '15 at 19:44
  • 5
    Use `exclude` property – basarat Nov 22 '15 at 23:47
  • Even with that change inside tsconfig.js TSL keeps underling in red your "duplicate" objects... so I had to restart VSCode for TSL behavior update... – A. Masson Jun 04 '16 at 18:13
  • This did not fix my problem :( what are the files we need to exclude? If I have `@types/react` and `@types/react-router-dom` I get this error: https://stackoverflow.com/questions/52323056/typescript-error-duplicate-identifier-librarymanagedattributes – Leon Gaban Sep 13 '18 at 23:53
  • Please note that if you use incremental builds, you should wipe your `.tsbuildinfo` files before trying to fix the problem. It took me a while to figure out that my problem was long solved, and I was only still getting this error because of the corrupted/outdated info in `.tsbuildinfo`. – loopmode Nov 25 '19 at 19:58
  • certify that you don't have both @types/node and ts-node installed at the same time; It fixed for me to check my package.json and remove all old clutter – Felype Feb 07 '20 at 17:28
  • another possible fix is to add `"skipLibCheck": true` to `compilerOptions` of tsconfig.json – Kaolin Fire Dec 29 '22 at 21:23
  • I got to a point where this was SO bad that I had to just create a new project and copy and paste the code inside. It worked which tells me installing something made this happen. I need to be more careful on what I install or uninstall from my project. – DaveK Apr 19 '23 at 12:02
74

Update: Version 1.0 of Typings changed the output structure and the below answer relates to pre 1.0 version.

If you are using Typings and exclude in your tsconfig.json, you may run into the issue of duplicate types and need something like the following:

{
  "exclude": [
    "typings/browser.d.ts",
    "typings/browser",
    "node_modules"
  ]
}

To simplify integration with TypeScript, two files - typings/main.d.ts and typings/browser.d.ts - are generated which reference all the typings installed in the project only one of which can be used at a time.

So depending on which version you need, you should exclude (or include) the "browser" or the "main" type files, but not both, as this is where the duplicates come from.

This Typings issue discusses it more.

  • Awesome man, this solves the issue with the duplicate identifier error from including [Typings](https://github.com/typings/typings) in your project :) – magicode118 Mar 10 '16 at 10:09
  • why not just exclude "typings"? The typings are only for dev time right, not for run time? – Eric Hartford Mar 27 '16 at 06:32
  • This is defining where to find the types at compile time. If you exclude "typings" you can no longer find any types. There problem here is it is trying to compile and there are duplicate types. TypeScript is not a runtime, it transpiles your code to JavaScript. – RationalDev likes GoFundMonica Mar 27 '16 at 07:43
  • 2
    In VS2017 I had to exclude "bin", "obj" in .NETCore2 MVC app – OSP Oct 31 '17 at 09:07
  • I had the same issue as OSP on this one. The obj folder had the TypeScript files in them, so they were showing up as duplicates. It caused problems the first time I built every time and then kept causing problems every time I built once I changed the build type. – Grungondola Aug 10 '18 at 19:04
33

Problem was solved by simply:

  1. Deleting the node_modules folder
  2. Running npm install to get all packages with correct versions

In my case, the problem occurred after changing Git branches, where a new branch was using a different set of node modules. The old branch was using TypeScript v1.8, the new one v2.0

Alex Klaus
  • 8,168
  • 8
  • 71
  • 87
  • 1
    Yay this worked for me. I wasn't able to delete node_modules at first because it says it needs Administrator access to delete it. My user account has administrator access; the real issue was that I needed to close programs that were using the folder. – Drew Jan 18 '19 at 18:53
  • it causes me many issues with compilation – Adir Dayan Nov 30 '20 at 09:13
  • 1
    It worked for me. I just removed the *.lock file and install again. – hobbydev Oct 05 '21 at 16:16
27

If you have installed typings separately in the typings folder add this to tsconfig.json:

{
  "exclude": [
    "node_modules",
    "typings"
  ]
}
isherwood
  • 58,414
  • 16
  • 114
  • 157
Lovjith
  • 355
  • 5
  • 7
  • 1
    I had a copy under /obj as well, which caused errors coming from "Typescript Virtual Projects". Adding "obj" to exclude fixed those. – Jon Oct 17 '16 at 14:22
  • This fully breaks my build sind the typings are missing. – Spenhouet Sep 17 '18 at 13:48
  • Worked to my case, where I'm in a Cordova project. I had to exclude "platforms" folder also. – Cesar Dec 25 '20 at 14:35
17

You could also use the exclude option in tsconfig.json file like so:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ]
}
thitemple
  • 5,833
  • 4
  • 42
  • 67
  • 4
    I'having this exact same issue. This fix does not work for me. I'm using Typescrip 1.75. I posted [an issue in the Typescript Github repo](https://github.com/Microsoft/TypeScript/issues/6489). – Bart Jan 14 '16 at 20:22
  • 1
    If you are using typings and not TSD you should use the solution proposed by @RationalDev – thitemple Mar 22 '16 at 16:24
15

I just ran into this problem. When I ran npm start, I got a bunch of duplicate identifier errors.

SOLUTION:

From the project root folder run:

rm -r typings
typings install
npm start

and everything works fine.

Molda
  • 5,619
  • 2
  • 23
  • 39
user6217332
  • 151
  • 2
  • 2
11

Enabling skipLibCheck in tsconfig fixed it for me.

{
  "compilerOptions": {
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.ts"]
}
lesterfernandez
  • 216
  • 3
  • 7
4

In my case I got the error as

node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.

And I had @types/es6-promise on my package.json but my tsconfig was already with target: "es6". So I guess there was a conflict with Promise when compiling.

Removing @types/es6-promise from my package.json file solved the issue.

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
4

Add "skipLibCheck": true to the "compilerOptions"

https://github.com/ionic-team/capacitor/issues/5436#issuecomment-1077942409

  • Sure, if you're aware of the implications of disabling type consistency checks for all your dependencies. This is not a good solution. – JHH Nov 09 '22 at 15:42
3

Using webpack I came across same error, just in case excluding the .d.ts file in your tsconfig.json and node_modules solved my issue:

"exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts",
    "typings/index.d.ts"
] 
kalifa17
  • 137
  • 7
3

Adding "typeRoots": ["node_modules/@types"], to "compilerOptions" in tsconfig.json file worked for me.

Patryk Janik
  • 2,476
  • 2
  • 13
  • 23
1

I had this issue caused by having an unexpected folder on disk (jspm_packages, no longer being used) which was not tracked by source control (and hidden from my IDE). This had a duplicate install of TypeScript in it, which caused the issues.

Bit of an edge case but leaving an answer here just in case someone else is hunting for this solution.

elwyn
  • 10,360
  • 11
  • 42
  • 52
  • Similar to me. I renamed a typescript file in Visual Studio Code which caused an invisible (orphansed) .js file that no longer showed in the IDE but was showing the duplicate identifier error nonetheless. – DAG Apr 21 '17 at 01:43
1

run the following command will fix this issue.

npm install @types/node --save-dev

Natarajan Ganapathi
  • 551
  • 1
  • 7
  • 19
0

I had this problem and it turns out I had a a second node_modules folder in my project that wasn't supposed to be there :-(

Bruno
  • 533
  • 1
  • 6
  • 27
  • There are some libraries that have node_modules inside their folders. when i was using node 4.6.0 it never created any problem, but with node 6.9.0 it suddenly is throwing that error. – heman123 Nov 29 '16 at 09:42
  • Wonder what version this issue started with. I had the issue with node 5.6.0. – Bruno Nov 29 '16 at 15:01
0

I had this error, along with others, after I changed my tsconfig.json to target:"es2015", and module:"es2015".

The base (AngularJS2 quickstart) used /// <reference path="../../typings/index.d.ts" /> in the main.ts file. To solve this, I had to remove that line.

Sako73
  • 9,957
  • 13
  • 57
  • 75
0

we removed a lib folder from the website folder. this was created by a previous installation of typings. this became duplicate. When this was removed it worked!

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
0

It can be because of having both typing and dependency in your node folder. so first check what you have in your @types folder and if you have them in dependencies, remove the duplicate. for me it was core.js

Mahdi Shahbazi
  • 1,063
  • 1
  • 10
  • 13
0

remove this @types/express-validator from package.json file, then run npm install

Read More

Author message: This package has been deprecated This is a stub types definition for express-validator (https://github.com/ctavan/express-validator). express-validator provides its own type definitions, so you don't need @types/express-validator installed!

Chanaka
  • 11
  • 2
0

Closing the solution completely and rerunning the project solved my issue.

karan chavan
  • 215
  • 2
  • 12
0

A (possible) solution for Angular

In my case, when installing @angularfire. It automatically imported environment from my environments file. Which gave the Duplicate identifier 'environment' error.

However, I already had this import in the same file, so that caused the duplicate identifier error.

It took me creating a new project and going step by step to find this error...

TL;DR: Check for duplicate imports in the file the error occurs.

Ruben Szekér
  • 1,065
  • 1
  • 10
  • 21
-2

I ran into a similar problem. Simply moving my tsconfig.json from the root of my project up to a different scope helped. In my project, I moved tsconfig.json from the root up to wwwroot.