Duplicate identifier 'LibraryManagedAttributes'

ReactjsTypescript

Reactjs Problem Overview


I have the same issue as in:

https://stackoverflow.com/questions/52339225/react-typescript-2312-14-duplicate-identifier-librarymanagedattributes

and

https://stackoverflow.com/questions/52323056/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?

Reactjs Solutions


Solution 1 - Reactjs

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.

Solution 2 - Reactjs

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.

Solution 3 - Reactjs

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

Solution 4 - Reactjs

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.

Solution 5 - Reactjs

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.

Solution 6 - Reactjs

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

Solution 7 - Reactjs

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.

Solution 8 - Reactjs

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.

Solution 9 - Reactjs

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

Solution 10 - Reactjs

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.

Solution 11 - Reactjs

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.

Solution 12 - Reactjs

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.

Solution 13 - Reactjs

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.

Solution 14 - Reactjs

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

Solution 15 - Reactjs

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

Solution 16 - Reactjs

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpenhouetView Question on Stackoverflow
Solution 1 - ReactjsSander SchuttenView Answer on Stackoverflow
Solution 2 - ReactjsSpenhouetView Answer on Stackoverflow
Solution 3 - ReactjschosenjuanView Answer on Stackoverflow
Solution 4 - ReactjsmongkuenView Answer on Stackoverflow
Solution 5 - ReactjsRichard TorcatoView Answer on Stackoverflow
Solution 6 - ReactjszenohView Answer on Stackoverflow
Solution 7 - ReactjsBenny NeugebauerView Answer on Stackoverflow
Solution 8 - ReactjsShan PlourdeView Answer on Stackoverflow
Solution 9 - ReactjszerocewlView Answer on Stackoverflow
Solution 10 - ReactjstsaiView Answer on Stackoverflow
Solution 11 - ReactjsHonza P.View Answer on Stackoverflow
Solution 12 - ReactjsbonnissView Answer on Stackoverflow
Solution 13 - ReactjsThiemo MüllerView Answer on Stackoverflow
Solution 14 - ReactjstrusktrView Answer on Stackoverflow
Solution 15 - ReactjsJeff TianView Answer on Stackoverflow
Solution 16 - ReactjsAlexJView Answer on Stackoverflow