我打算创建一个npm包(recharts-candlestick)。我的包唯一依赖的是Recharts。我已经创建了这个包,使用Webpack来构建它,并使用npm link在本地测试了这个包。这工作时,我不使用Recharts,但一旦我有Recharts作为对等依赖,我得到一个错误消息(https://reactjs.org/docs/error-decoder.html/?invariant=321)。我认为我有这个问题,因为我有多个版本的反应:
$ 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
我已经尝试通过将包中的react版本链接到我正在测试的项目的react包(https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react)来修复本地重复的react包。然而,我怀疑目前的设置不会有任何错误,因为其他的react版本都被欺骗了。这个包的源代码可以在这里找到:https://github.com/jobdenotter/candlestick-chart。包。包代码的Json为:
"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"
}
}
这个问题发生在有一个全局的react版本,然后与应用程序的本地版本冲突的时候
我相信这里有详细的答案:https://stackoverflow.com/a/71416403