Definitelytyped: Duplicate identifier with @types/node 10.0

Created on 26 Apr 2018  ·  40Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

If you know how to fix the issue, make a pull request instead.

  • [x] I tried using the @types/xxxx package and had problems.
  • [ ] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [ ] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [ ] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @.types/node 10.0

With v9.6.6 everything was fine with my Angular project. Upgraded to 10.0.0 and now I get the following errors running my unit tests:

Error: node_modules/@types/node/index.d.ts(2381,15): error TS2300: Duplicate identifier 'URL'. node_modules/@types/node/index.d.ts(2399,15): error TS2300: Duplicate identifier 'URLSearchParams'. node_modules/@types/node/index.d.ts(2417,14): error TS2661: Cannot export 'URL'. Only local declarations can be exported from a module. node_modules/@types/node/index.d.ts(2417,19): error TS2661: Cannot export 'URLSearchParams'. Only local declarations can be exported from a module. node_modules/typescript/lib/lib.dom.d.ts(12210,11): error TS2300: Duplicate identifier 'URL'. node_modules/typescript/lib/lib.dom.d.ts(12226,13): error TS2300: Duplicate identifier 'URL'. node_modules/typescript/lib/lib.dom.d.ts(14282,11): error TS2300: Duplicate identifier 'URLSearchParams'. node_modules/typescript/lib/lib.dom.d.ts(14309,13): error TS2300: Duplicate identifier 'URLSearchParams'.

Most helpful comment

I will be able to look at this tonight.

All 40 comments

I was just about to log this same issue!

I tried this:

npm init
npm i -g typescript
npm i --save-dev @types/node
tsc --init
touch index.ts
tsc

I get these same errors.

Adding additional mention for the latest author: @rbuckton.

I am also seeing this. The last push broke our build with this same error.

Also getting this. Looks like a conflict with Typescript in my case.
Fixed it by downgrading to v9.6.7

Yes. In the OP's post, the conflict is with lib.dom.d.ts. I also see conflits with lib.d.ts and lib.es2017.full.d.ts.

yep. seeing this as well.

Experienced this issue as well, fixed by downgrading @types/node to v9.6.7

We're still using the latest LTS (8), so we solved this (for now) by using ^8.0.0.

I will be able to look at this tonight.

You can get an idea of the impact of this breaking change with @dependabot Dependabot compatibility score
Probably most TS users are including @types/node which is causing the duplicate identifier and failing CI.

@rbuckton Thanks for looking into this 👍

Quick fix:

npm i @types/[email protected]

For old-fashioned guys with nodejs v6.x.x - npm i --save-dev @types/node@6

This was broken because these classes were changed to be globally exported, line 2380 in the current file. See the history comparison below:

https://github.com/DefinitelyTyped/DefinitelyTyped/commit/bffb03282272b37ccb12429026f4504a39a3cd83#diff-7d84e08967cded0b99ed4328aab0a1a8

I was just about to pull and make the changes but looks like @rbuckton is already ahead of me in the process.

@robertbradleyux In node 10.0.0, The URL class is also available on the global object so this seems correct.

@rbuckton Is there a way to tell TypeScript to only define the class if it is not already defined?

In this case, both node and the browser implement the same WHATWG URL Standard so this is similar to setTimeout which has a different return type in Node.js than the browser https://nodejs.org/dist/latest-v10.x/docs/api/timers.html

Somehow TypeScript is able to handle the setTimeout so surely it could handle the global URL, right?


Update I see there is already a pull request to revert this change https://github.com/DefinitelyTyped/DefinitelyTyped/pull/25356

After clearing my npm cache I am still getting the original error

@kgorlick This won't show up until the types publisher publishes the updated types to npm.

@styfle, NodeJS and DOM have slight differences in their implementations of the WHATWG URL standard (e.g. the NodeJS version has a toJSON() that isn't in the DOM version, and the DOM version has a static createObjectURL method that does not appear in the NodeJS version).

I temporary fix the issue adding
"lib": ["es6"],
in my tsconfig.json

@rbuckton I understand their might be some differences in the URL definitions.

setTimeout has a different return type in Node.js than the browser: docs

Yet somehow, including @types/node doesn't break the build. How does that one work?

IIRC, the multiple versions of the timer APIs become overloads. The problem with URL is that it is an object type.

Got this error on @11.9.3 and fixed by:
npm i @types/[email protected] --save-dev

Getting this error
with "@types/node": "^11.9.4",

node_modules/@types/node/index.d.ts:75:15 - error TS2300: Duplicate identifier 'SharedArrayBuffer'.

75 declare class SharedArrayBuffer {
~~~~~

node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts:24:11
24 interface SharedArrayBuffer {
~~~~~
'SharedArrayBuffer' was also declared here.
node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts:46:13
46 declare var SharedArrayBuffer: SharedArrayBufferConstructor;
~~~~~
and here.

node_modules/@types/node/index.d.ts:83:15 - error TS2451: Cannot redeclare block-scoped variable 'custom'.

etc.....

@Mulperi I think your issue is different to the above and related to the change of interface SharedArrayBuffer to class SharedArrayBuffer done in #32878.

@SimonSchick I think this should be changed back as it is actually just a forward declaration.

@Flarna this will only work if you load dom typings tho, node typings should work without loading dom typings..

It's not dom it's es2017.sharedmemory which is included in 3.1 typings via /// <reference lib="es2018" /> but older typescript versions do not support the reference lib directive.
Forward declaration is used to avoid that need the everyone has to include it manually. Forward declarations shall be an interface as interfaces merge with classes whereas classes are not allowed to be defined twice. So using class for a forward declaration breaks builds where the lib with the real definition is included.

@Flarna would simply adding /// <reference lib="es2017.sharedmemory" /> to /index.d.ts work in this case?

Also relevant since Atomics is still missing from those typings 🤔

Nope as /// <reference lib has been introduced in typescript 3.0 and the effected file is that one which is used for 2.1...3.0.
@Mulperi Which typescript version do you use? I expect it's <3.1.

I'm also getting this problem. And changing to a previous version of @types/node is not a feasible option, since @types/connect uses *, pulls @^11.9, and causes conflicts in the test suite.

@Flarna I'm currently travelling, could you perhaps look into this?

@jmeberlein Which typescript version do you use? I expect it's <3.1.

created #33177. Hard to tell when this get's merged as CI is in a bad state currently.

Typescript version ^2.9.2 in package.json, resolving to 2.9.2 in npm-shrinkwrap.json

why is this story closed? It seems to be broken still.

why is this story closed? It seems to be broken still.

This was resolved almost a year ago, so it seems it was fixed but has now regressed. I'd probably open a new issue.

ng] ERROR in node_modules/@types/node/index.d.ts:73:11 - error TS2300: Duplicate identifier 'IteratorResult'. [ng] 73 interface IteratorResult<T> { } [ng] ~~~~~~~~~~~~~~ [ng] node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 [ng] 41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>; [ng] ~~~~~~~~~~~~~~ [ng] 'IteratorResult' was also declared here. [ng] node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 - error TS2300: Duplicate identifier 'IteratorResult'. [ng] 41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>; [ng] ~~~~~~~~~~~~~~ [ng] node_modules/@types/node/index.d.ts:73:11 [ng] 73 interface IteratorResult<T> { } [ng] ~~~~~~~~~~~~~~ [ng] 'IteratorResult' was also declared here.

Problem still not resolved

@thameurr Please note that you comment on an issue closed quite a while ago. I assume you use a different set of @types/node, typescript,... versions then that ones mentined in this issue.

Could you create a new issue with instructions how to reproduced where up to date versions of the packages are used?

I got this problem with @types/node: "13.9.5" and it was fixed by rolling back to "12.12.31" 🤷‍♂️

Type error: Duplicate identifier 'ProcessEnv'

This issue was still occurring for me with TypeScript 4.0.2. Installing @types/node at version 14.6.0 seemed to make the errors go away when running tsc --noEmit.

Was this page helpful?
0 / 5 - 0 ratings