123

I have the same issue as in:

React typescript (2312,14): Duplicate identifier 'LibraryManagedAttributes'

and

TypeScript error: Duplicate identifier 'LibraryManagedAttributes'

But I just can't find any solution.

I already upgraded to the latest node/npm/yarn/typescript versions. Also tried downgrading. Nothing helps.

yarn build --verbose
yarn run v1.9.4
$ react-scripts-ts build --verbose
Creating an optimized production build...
Starting type checking and linting service...
Using 1 worker with 2048MB memory limit
ts-loader: Using typescript@3.0.3 and C:\dev\project\frontend\tsconfig.prod.json
Warning: member-ordering - Bad member kind: public-before-private
Failed to compile.

C:/dev/project/frontend/node_modules/@types/prop-types/node_modules/@types/react/index.d.ts
(2312,14): Duplicate identifier 'LibraryManagedAttributes'.


error Command failed with exit code 1.

--verbose somehow doesn't give me more information.

As I can see LibraryManagedAttributes is defined in:

  • node_modules/@types/react/index.d.ts
  • node_modules/@types/prop-types/node_modules/@types/react/index.d.ts
  • node_modules/@types/react-overlays/node_modules/@types/react/index.d.ts
  • ....

Where is this coming from? How can I avoid that?

I want to find out where this error is coming from so that I can report it to the right entity but I don't know where to start.

What else can I try?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Spenhouet
  • 6,556
  • 12
  • 51
  • 76
  • Yarn shouldn't be installing multiple copies of `@types/react` like that. Try deleting your `yarn.lock` file and running `yarn install` again. If that doesn't help, try removing all dependencies from `package.json` except `@types/react`, `@types/prop-types`, and `@types/react-overlays` and see if you still get the problem. If so, add your `package.json` and `yarn.lock` (it should be no more than ~40 lines) to the question so we can see why yarn is doing what it is doing. – Matt McCutchen Sep 20 '18 at 03:52
  • Deleting the yarn.lock file actually helped. I'm still worried this just repeats at some day. I'm not sure how this came to be in the first place. It might be related to this issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/28881 – Spenhouet Sep 20 '18 at 05:57
  • 1
    If anyone is facing an issue because of @types/react-dom internal dependency and @types/react clashing, you can try this answer, which basically uninstalled and reinstalled them both in a single go. https://stackoverflow.com/a/54239498/4353782 – Mayur Dhurpate Apr 11 '20 at 13:22

17 Answers17

173

This seems te happen because Yarn resolves multiple versions of a package; @types/react in this particular case. Yarn resolves @types/react from your package.json and as a dependency of @types/react-dom.

Take the following snippet from my package.json:

"devDependencies": {
  "@types/react": "^15.0.16",
  "@types/react-dom": "^0.14.23"
  ...
}

The yarn.lock that is created after you run yarn install contains something similar to this:

"@types/react-dom@^0.14.23":
  version "0.14.23"
  resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
  dependencies:
    "@types/react" "*"

"@types/react@*":
  version "16.4.14"
  resolved "https://registry.yarnpkg.com/@types/react/-/react-16.4.14.tgz#47c604c8e46ed674bbdf4aabf82b34b9041c6a04"
  dependencies:
    "@types/prop-types" "*"
    csstype "^2.2.0"

"@types/react@^15.0.16":
  version "15.6.19"
  resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"

Notice that @types/react-dom depends on any version of @types/react as indicated by "*". Yarn resolves two versions of @types/react: "16.4.14" and "15.6.19". This results in the type conflicts you mentioned.

The solution is to add a resolutions field to your package.json to tell Yarn to resolve a specific version of @types/react. Take the following sample:

"resolutions": {
  "@types/react": "^15.0.16"
}

Run yarn install again. Notice the change in the yarn.lock file:

"@types/react-dom@^0.14.23":
  version "0.14.23"
  resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
  dependencies:
    "@types/react" "*"

"@types/react@*", "@types/react@^15.0.16":
  version "15.6.19"
  resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"

Yarn now resolves the same version "15.6.19" for both "@types/react@*" and "@types/react@^15.0.16" dependencies.

I would like to know myself why this is needed. I would expect Yarn to understand it can resolve dependency "@types/react" "*" with "@types/react@^15.0.16" instead of resolving it with the latest version of @types/react.

Sander Schutten
  • 1,862
  • 1
  • 10
  • 10
  • 1
    I had the same problem, and this answer brought me closer to the solution. However, I also had to delete the `node_modules` folder before `yarn install`, since otherwise `yarn install` would not act sufficiently on the changed `package.json`. (I edited the answer accordingly to keep other readers from running into that problem, but strangely that got peer-rejected.) – Carsten Führmann Nov 18 '18 at 14:40
  • 7
    Once the lockfile has been updated is it safe to remove the `resolutions` field? – harryg Feb 06 '19 at 14:07
  • 1
    Yes, it's safe to remove the resolutions config once the new lock file has been created. – JMadelaine Apr 21 '20 at 04:06
  • @sander schutten Thanks, I have created a bug report for React team https://github.com/facebook/react/issues/20005 – Tom Smykowski Oct 13 '20 at 08:24
  • 4
    You can use `yarn why @types/react` to get additional information of where these differing versions are coming from. In my case I had a 17.x and 16.x version, the latter of which came from a dependency I didn't actually use so I could just remove it. – Jeroen Vannevel Dec 21 '20 at 21:56
47

This seems to be a typescript issue.

My current workaround is adding "skipLibCheck": true to tsconfig.json.

I want to stress that that is only a workaround and not a fix to the problem it self.

Spenhouet
  • 6,556
  • 12
  • 51
  • 76
  • 2
    Has this been resolved? I just tried to create and application and received this error. – MickB Nov 16 '18 at 21:59
  • 23
    Note that adding `"skipLibCheck": true` to tsconfig is a potentially destructive action on the type-safety of your codebase, and is very hard to undo. – cnp May 30 '19 at 20:28
  • This should be the answer – HalfWebDev Oct 08 '19 at 07:18
  • Saved my day, enabling skipLibCheck works for me , For me it was not working only in my local so enabling skipLibCheck flag should fine for me – Raj Feb 12 '20 at 09:51
28

I got the same error. I managed to fixed it by removing my '@types/react' and then installing them again.

yarn remove @types/react
yarn add --dev @types/react
Henrik
  • 9,714
  • 5
  • 53
  • 87
chosenjuan
  • 289
  • 3
  • 2
24

For me I had react types duplicated in react-redux, react, and react-intl when I upgraded react-intl. The least intrusive fix that's worked for me so far is to run this:

npx yarn-deduplicate --packages @types/react yarn.lock

If the resulting diff of the lockfile looks correct, go ahead and delete node_modules, then yarn to get fresh packages off the deduplicated lockfile.

mongkuen
  • 241
  • 2
  • 5
10

In my case the error showed up when the version numbers of @types/react (v17.0.3) and @types/react-dom (v17.0.2) have not been in sync.

To solve the issue, I removed @types/react because it gets hoisted from @types/react-dom. You can verify that by executing yarn why @types/react.

Benny Code
  • 51,456
  • 28
  • 233
  • 198
7

The easiest way to fix this for me was to delete my node_modules directory and yarn.lock/package-lock files and then do a yarn install to reinstall all the node modules.

Richard Torcato
  • 2,504
  • 25
  • 26
5

Related to the question, running npm list @types/react from the directory of your package.json should list duplicate type definitions found in your project.

Shan Plourde
  • 8,528
  • 2
  • 29
  • 42
5

In our case, we fixed it by

  1. Moving all @types/* packages to devDependencies

  2. rm -rf yarn.lock and rm -rf node_modules

  3. Run yarn install again

zenoh
  • 2,071
  • 1
  • 22
  • 38
4

Using yarn-deduplicate fixed the issue for me.

Steps:

  1. Optional install yarn-deduplicate package or use npx in step two
npm install -g yarn-deduplicate

or

yarn global add yarn-deduplicate
  1. Run yarn-deduplicate
yarn-deduplicate yarn.lock --packages @types/react yarn.lock

or

npx yarn-deduplicate --packages @types/react yarn.lock
  1. Remove node_modules folder
rm -rf node_modules
  1. Reinstall dependencies
yarn install
zerocewl
  • 11,401
  • 6
  • 27
  • 53
2

I have the same issue after yarn upgrade @types/react-router-dom. git diff shows multiple versions of @types/react resolved. In my case, yarn upgrade @types/react resolves the issue. Removing yarn.lock should help.

It seems a fresh (without yarn.lock) install would resolve packages to a consistent state, but a partial upgrade would not resolve the dependencies globally. Thus manual tweaks may be necessary to upgrade all involved packages.

tsai
  • 209
  • 1
  • 4
  • 2
    removing `yarn.lock` was the key. Deleting only `node_modules` and installing did not help. – oyalhi Dec 22 '18 at 06:32
  • 1
    `yarn upgrade @types/react` resolved the issue for me. I did not need to nuke the `yarn.lock` file – Zander Jan 18 '19 at 19:57
1

What worked for me was deleting react and @types/react from package.json, then in zsh:

rm -rf node_modules/**/react
npm i react @types/react
trusktr
  • 44,284
  • 53
  • 191
  • 263
1
C:/Users/japa/source/repos/ReactTestApp/TemplateExample/ClientApp/node_modules/@types/react/index.d.ts
TypeScript error in C:/Users/japa/source/repos/ReactTestApp/TemplateExample/ClientApp/node_modules/@types/react/index.d.ts(2835,14):
Duplicate identifier 'LibraryManagedAttributes'.  TS2300

In my case I needed to solve the problem manually (using principle described in TS2300). The problem arose once I added ReactKendo to my project.

  1. Went to ClientApp directory in my project ClientApp\node_modules\@types
  2. Backed up the react directory and then deleted it
  3. Clean + Build + Run project and no more above error occures
  4. I recovered the react folder after the bug disappeared and error seems to be gone forever, so it seems to me like typical magic bug somewhere in the universe :-)

I did not need to change anything else in config files.

Honza P.
  • 1,165
  • 1
  • 11
  • 12
1

If you're using npm instead of yarn and try to fix your @types/react dependency problems by adding the following to your package.json :

"resolutions": {
  "@types/react": "x.x.x"
}

You might still end up with the same error. With npm, you should be using overrides instead :

"overrides": {
  "@types/react": "x.x.x"
}

This might help someone trying to fix their issue with the recommendations of this stream while working with npm instead of yarn. Note that you will still need to delete your node_modules and your package-lock.json before running npm install for the changes to be applied.

Raphasse
  • 61
  • 5
0

In my case, I got the error indicate that 'LibraryManagedAttributes' is declared in 2 different locations. Follow the paths, I realized that an installed module also has a package.json file which also add "@types/react" as dep, and its version is not the same as the one in the root package.json file. I changed these two to the same version and the problem was solved.

bonniss
  • 445
  • 5
  • 13
  • This is an old answer, but given it ranks highest for me under "active" I feel it deserves the note: you shouldn't ever modify the package.json file of a dependency. As soon as you reinstall node modules, or another dev builds from the source, your changes will not propagate and you will have a bad time. – sean.hudson Jun 18 '19 at 08:27
0

I had a conflicting version request for react in another module I used. Fixing that and re-installing with yarn didn't help me either.

Using NPM instead of Yarn however solved it for me.

Hope this helps someone.

0

For me, it caused by only referenced @types/react-redux. Fixed by npm i --save-dev @types/react, and so the package.json looks like this:

  ...
  "devDependencies": {
    "@types/react": "^16.9.19",
    "@types/react-redux": "^7.1.7"
    ...
  }
Jeff Tian
  • 5,210
  • 3
  • 51
  • 71
0

I had this issue while working with linked dependencies. My linked package lives in a lerna repo, and it had @types/react as a devDependency. I added @types/react as a peerDependency, switched my workflow to yalc, and was able to continue.

AlexJ
  • 87
  • 1
  • 10