278

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"
    }
  }
}
3
  • Your tsd.d.ts probably looks like a couple of reference tags, and not a JSON file. Commented Jan 7, 2016 at 6:14
  • 1
    Why do you need a typings.json AND a tsd.d.ts? Commented Aug 19, 2016 at 20:28
  • I put the solution here, you can see. stackoverflow.com/questions/40404353/… Commented Jul 30, 2017 at 20:05

22 Answers 22

149

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 🌹

Sign up to request clarification or add additional context in comments.

11 Comments

Or install typescript globally instead?
Yeah I don't know why its there locally :) I assumed perhaps he needed some api from it (in hindsight ... its unlikely)
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!
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.
Use exclude property
|
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.

5 Comments

Awesome man, this solves the issue with the duplicate identifier error from including Typings in your project :)
why not just exclude "typings"? The typings are only for dev time right, not for run time?
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.
In VS2017 I had to exclude "bin", "obj" in .NETCore2 MVC app
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.
35

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

3 Comments

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.
it causes me many issues with compilation
It worked for me. I just removed the *.lock file and install again.
29

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

{
  "exclude": [
    "node_modules",
    "typings"
  ]
}

3 Comments

I had a copy under /obj as well, which caused errors coming from "Typescript Virtual Projects". Adding "obj" to exclude fixed those.
This fully breaks my build sind the typings are missing.
Worked to my case, where I'm in a Cordova project. I had to exclude "platforms" folder also.
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"
  ]
}

2 Comments

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.
If you are using typings and not TSD you should use the solution proposed by @RationalDev
17

Enabling skipLibCheck in tsconfig fixed it for me.

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

Comments

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.

3 Comments

For me, I was switching from using es6-shim to using core-js for my polyfills, and this is the solution that would have worked. A less heavy-handed approach is to use typings prune.
Works for me. But guess there should be 'npm install' instead of 'typings install'
I have no typings folder :(
6

Add "skipLibCheck": true to the "compilerOptions"

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

1 Comment

Sure, if you're aware of the implications of disabling type consistency checks for all your dependencies. This is not a good solution.
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.

Comments

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"
] 

1 Comment

I had this error with webpack too, just restarting the server fixed it.
3

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

Comments

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.

1 Comment

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.
1

run the following command will fix this issue.

npm install @types/node --save-dev

Comments

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 :-(

2 Comments

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.
Wonder what version this issue started with. I had the issue with node 5.6.0.
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.

Comments

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!

Comments

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

Comments

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!

Comments

0

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

1 Comment

Or just recheck the typescript errors in the Typescript tab in IntelliJ
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.

Comments

0

Not directly answering this question, but if you have this error, be sure to check you didn't copy paste the file contents twice over accidentally :skull:

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.