163

I'm trying to compile via tsc--which I've installed globally--and I'm getting an error:

~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 - error TS2300: Duplicate identifier 'IteratorResult'.

41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
        ~~~~~~~~~~~~~~

  node_modules/@types/node/index.d.ts:170:11
    170 interface IteratorResult<T> { }
                  ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.

node_modules/@types/node/index.d.ts:170:11 - error TS2300: Duplicate identifier 'IteratorResult'.

170 interface IteratorResult<T> { }
              ~~~~~~~~~~~~~~

~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6
    41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
            ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.


Found 2 errors.

I have @types/node version 10.1.0 installed. (@latest has its own issues...)

tsconfig.json

{
  "compilerOptions": {
    "target": "es2018",
    "moduleResolution": "node",
    "module": "commonjs",
    "jsx": "react",
    "lib": [
      "dom",
      "es2018",
      "dom.iterable",
      "scripthost"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "./types"
    ],
    "types": [],

    "alwaysStrict": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,

    "sourceMap": true,

    "outDir": "dist"
  },
  "files": [
    "app/index.tsx"
  ],
  "include": [
    "app/**/*.ts",
    "app/**/*.tsx",
    "test/**/*.ts",
    "test/**/*.tsx",
    "node_modules/@types/**/*.d.ts",
    "./types/**/*.d.ts"
  ],
  "exclude": [
    "dist"
  ]
}

If I uninstall typescript globally and run npx tsc it works, but there should be nothing wrong with installing and running typescript globally. After all, that's the whole point of installing things globally.

In the meantime I have a workaround which is to just alias tsc (I'm using git bash in Windows).

alias tsc="path/to/project/node_modules/.bin/tsc.cmd"
Gregor Albert
  • 819
  • 1
  • 11
  • 23
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
  • this issue started popping up for me when i upgraded globally from v3.5.3 to v3.6.2 ... so guessing there there is something which is changed there. rollback helped or having script "build": "tsc" in package.json and using npm run build helped, where typescript in my local package.jon was on v3.5.3 so npm uses that. – Anant Anand Gupta Sep 14 '19 at 12:41

13 Answers13

229

Found an issue on GitHub - https://github.com/microsoft/TypeScript/issues/32333 which was related. @rbuckton suggested upgrading @types/node. It worked for me.

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
OldMan
  • 2,466
  • 1
  • 11
  • 5
  • 51
    `npm install --save-dev @types/node` – coverboy Nov 30 '19 at 13:09
  • 31
    `npm update --save-dev @types/node` is more correct as it may well be a minor version upgrade, see [install vs update](https://stackoverflow.com/questions/12478679/npm-install-vs-update-whats-the-difference) – Taran Dec 13 '19 at 06:37
  • 25
    I was getting this error running storybook after upgrading my project to Angular 9. I presumed a simple `npm install @types/node` would install the version of types/node that I already have in my package.json, so I did `npm install @types/node@latest` and it worked. – Muhammad bin Yusrat Feb 13 '20 at 04:21
  • After that I got error TS2320: Interface 'NodeRequire' cannot simultaneously extend types 'Require' and 'RequireFunction' – Travnikov.dev Mar 11 '20 at 14:45
  • Had to remove `node_modules` and install them again to have it working, but indeed that was the problem! – Daniel Danielecki Sep 27 '20 at 16:21
  • Can confirm @Muhammad that it worked for me too :-) – Kieran Ryan Dec 06 '20 at 22:36
  • For some reason this is only working for the first 'ng build' I do and then goes right back to failing. Not sure what that entails. – carbon1479 Aug 25 '21 at 18:33
41

I was getting the is error in my angular 8 App and couldn't resolve the issue after trying all suggestions made here including the accepted answer. I had to look at a previous angular 6 App that compiled without errors and realised that I could just skip the library check by including

"skipLibCheck": true

to the tsconfig.json file. With the fact that my App is running well without problems, I decided to take this approach. Here is the complete configuartion of my tsconfig.json file

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "module": "esnext",
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2015",
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2018",
            "dom"
        ],
        "skipLibCheck": true
    },
    "angularCompilerOptions": {
        "fullTemplateTypeCheck": true,
        "strictInjectionParameters": true
    }
}

There were no more errors after this configuration. Note: That doesn't mean that the problem is solved but at least it allowed me to skip the bug that was causing the error. Due to the fact that my App is running as expected I just considered this error irrelevant at this moment.

omostan
  • 840
  • 8
  • 9
9

I suspect it is because your include section:

"include": [
    "app/**/*.ts",
    "app/**/*.tsx",
    "test/**/*.ts",
    "test/**/*.tsx",
    "node_modules/@types/**/*.d.ts",
    "./types/**/*.d.ts"
  ]

You usually don't need to explicitly include *.d.ts files. And probably never declaration files from other libraries (or node types).

tsconfig's "exclude" section excludes everything under "node_modules" by default (among other things). When you add "node_modules/@types/**/*.d.ts" you override that exclude and tsc tries to include them, but those types are already declared.

Check Typescript docs on tsconfig.json, it explains the "typeRoots", "files" and "include"/"exclude" config options in detail.

danielv
  • 3,039
  • 25
  • 40
  • 2
    For me, it was this line in tsconfig.json: ```"files": [ "./node_modules/@types/node/index.d.ts" ],``` which I taken from some tutorial. Your answer helped! – caffeinum Dec 10 '19 at 18:59
6

For me it turned out I had a node_modules folder in a parent directory project, something similar to this:

node_modules
my-project
- node_modules

Since the node_modules had an older version of @types/node installed, the problem happened. In my case the solution however wasn't to update @types/node but instead to remove those node_modules since I wasn't using them in the first place.

If you actually need to have a node_modules in a parent directory with different types and this is how you want it to be, then you can specify the typeRoots specifically:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es6",
    "declaration": true,
    "outDir": "./dist",
    "typeRoots": ["./node_modules/@types/"]
  },
  "include": [
    "src/**/*"
  ]
}

That way, the parent node_modules aren't scanned for types. Otherwise they are, read here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.

bersling
  • 17,851
  • 9
  • 60
  • 74
6

As @Muhammad bin Yusrat said in his comment, run npm i @types/node@latest (npm i @types/node doesn't work !!) if you have just updated angular to 9. That worked for me.

It also got rid of another ionic 5 console error after running ionic serve-> 'refused to load image 'http:localhost:8100/favicon.ico' because it violates the following Content Security Policy .....' (see below).

ionic 5 error after running ionic serve

Another 'IteratorResult' error was caused by "Spread Types" Error. See Typescript: Spread types may only be created from object types. Basically somewhere in your code you have used a spread operator like this return { id: doc.payload.id, ...doc.payload.data() }; and you have to change it to this return { id: doc.payload.id, ...doc.payload.data() as {} }; ie add as {}

Mark
  • 370
  • 5
  • 10
  • 3
    Just upgrade from Angular 8 to 9, ```npm install --save-dev @types/node@latest``` fixed the "IteratorResult" error, thanks!! – Nier Jul 31 '20 at 09:54
5

Add skipLibCheck: true in compilerOptions in tsconfig.json.

This solved the issue. Check here

Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
4

I just removed types/node by running

sudo npm remove @types/node

and installed again by running the following command and it worked for me.

sudo npm i @types/node
ASANIAN
  • 372
  • 2
  • 8
  • 2
    For anyone reading this answer, don't sudo your npm commands. It shouldn't be needed and you're giving way too much trust to scripts that exist as part of npm packages and libraries. – Keith Banner Feb 09 '22 at 17:22
0

Just upgrade @types/node in devDependencies of your Angular project:

 npm i --save-dev @types/node

*** Do not change anything in node_modules ***

Dharman
  • 30,962
  • 25
  • 85
  • 135
Anusha
  • 229
  • 1
  • 4
  • 8
0

This might help someone.

I had a similar issue upgrading Angular from v8.2.11 or v9.1.13. I was able to resolve the issue by following the steps

  1. Remove the node_modules directory and file package-lock.json
  2. Reinstall the dependencies using npm install
freebug
  • 129
  • 1
  • 5
0

In my case resolved by changing the package.json file following part.

"devDependencies": {
  "@angular-devkit/build-angular": "^15.2.0",
  "@angular/cli": "^15.2.7",
  "@angular/compiler-cli": "^15.2.8",
  "@types/jasmine": "~4.3.1",
  "@types/jasminewd2": "~2.0.10",
  "@types/node": "^18.7.11",
  "jasmine-core": "~4.6.0",
  "karma": "~6.4.2",
  "karma-chrome-launcher": "~3.2.0",
  "karma-coverage": "~2.2.0",
  "karma-jasmine": "~5.1.0",
  "karma-jasmine-html-reporter": "^2.0.0",
  "typescript": "~4.9.5"
},
Swati
  • 234
  • 2
  • 10
-1

This is how I solved it:

npm uninstall --save-dev webpack

npm install --save-dev @angular-devkit/build-angular@latest
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
ABO Baloyi
  • 17
  • 4
-2

I found this thread after googling the error. My problem was that somehow I had an unneeded import which caused this:

import { error } from 'protractor';
Spikolynn
  • 4,067
  • 2
  • 37
  • 44
-5

I resolved this issue manually by commenting one of the interface "IteratorResult" declaration in node_modules/@types/node/index.d.ts file. Hope this will help.