简体   繁体   中英

Prevent multiple versions of React (while creating npm package)

I intend to create an npm package ( recharts-candlestick ). The only dependency my package has is Recharts. I have created the package, used Webpack to build it and tested the package locally using npm link. This works when I do NOT use Recharts, but as soon as I have Recharts as a peer dependency I get an error message ( https://reactjs.org/docs/error-decoder.html/?invariant=321 ). I think I am having this issue because I have multiple versions of react:

$ npm ls react
test@0.1.0 C:\path_to_project
├─┬ react-dom@17.0.2
│ └── react@17.0.2 deduped
├─┬ react-scripts@5.0.0
│ └── react@17.0.2 deduped
├── react@17.0.2
└─┬ recharts-candlestick@1.0.3
  ├── react@17.0.2 deduped
  └─┬ recharts@2.1.9
    ├─┬ react-resize-detector@6.7.8
    │ └── react@17.0.2 deduped
    ├─┬ react-smooth@2.0.0
    │ ├─┬ react-transition-group@2.9.0
    │ │ └── react@17.0.2 deduped
    │ └── react@17.0.2 deduped
    └── react@17.0.2 deduped

I have tried fixing the duplicate react packages locally by linking the react version in the package to the react package of the project I am testing the package in ( https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react ). I would however suspect that with the current setup there would not be any errors anyway because the other react versions are all deduped. The source code for the package can be found here: https://github.com/jobdenotter/candlestick-chart . The package.json of the package code is:

  "name": "my-package",
  "version": "1.0.0",
  "description": "",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack --watch"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.16.7",
    "webpack": "^5.72.0"
  },
  "peerDependencies": {
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
    "recharts": "^2.1.9"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.2.4",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "webpack-cli": "^4.9.2"
  }
}

This issue occurs when there is a global react version in place, which then conflicts with the local version of the app

I believe it is answered at length here: https://stackoverflow.com/a/71416403

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM