如何解决由AWS Amplify引起的对等依赖问题?

8

我遇到了React构建方面的问题,可能与AWS Amplify有关,但我无法准确定位。

一开始,我可以在本地运行我的程序,没有任何问题。然后,我将它上传到GitHub,并使用Amplify进行CI/CD设置以构建它。Amplify说在构建过程中出现了错误。这时我发现我不能运行npm update。

我已经尝试很多方法,但可能进一步破坏了环境。如果有人有什么建议,我会非常感激。

我最初遇到的错误类似于以下内容:

npm WARN ERESOLVE overriding peer dependency
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR! 
npm ERR! While resolving: react-drag-drop-files@2.3.7
npm ERR! Found: react@16.14.0
npm ERR! node_modules/react
npm ERR!   peer react@"18.1.0" from react-native@0.70.6
npm ERR!   node_modules/react-native
npm ERR!     peer react-native@">=0.56" from react-native-get-random-values@1.7.0
npm ERR!     node_modules/react-native-get-random-values
npm ERR!       react-native-get-random-values@"^1.4.0" from @aws-sdk/middleware-retry@3.6.1
npm ERR!       node_modules/@aws-sdk/middleware-retry
npm ERR!         @aws-sdk/middleware-retry@"3.6.1" from @aws-sdk/client-cognito-identity@3.6.1
npm ERR!         node_modules/@aws-sdk/client-cognito-identity
npm ERR!         11 more (@aws-sdk/client-comprehend, ...)
npm ERR!     peer react-native@"^0.0.0-0 || 0.60 - 0.71 || 1000.0.0" from @react-native-async-storage/async-storage@1.17.11
npm ERR!     node_modules/@react-native-async-storage/async-storage
npm ERR!       peer @react-native-async-storage/async-storage@"^1.13.0" from @aws-amplify/core@4.0.2
npm ERR!       node_modules/@aws-amplify/core
npm ERR!         @aws-amplify/core@"4.0.2" from @aws-amplify/analytics@5.0.0
npm ERR!         node_modules/@aws-amplify/analytics
npm ERR!         11 more (@aws-amplify/api-graphql, @aws-amplify/api-rest, ...)
npm ERR!       1 more (amazon-cognito-identity-js)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.0.0" from react-drag-drop-files@2.3.7
npm ERR! node_modules/react-drag-drop-files
npm ERR!   react-drag-drop-files@"^2.3.7" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: react@18.2.0
npm ERR! node_modules/react
npm ERR!   peer react@"^18.0.0" from react-drag-drop-files@2.3.7
npm ERR!   node_modules/react-drag-drop-files
npm ERR!     react-drag-drop-files@"^2.3.7" from the root project

完全卸载react-drag-drop-files并没有帮助,它仅用另一个依赖问题替代了它;
我看到的建议大多是在执行npm config set legacy-peer-deps=true后重新安装。那似乎不是理想的解决方法,但我找不到如何实际解决依赖冲突的具体描述。我尝试卸载和重新安装相关模块,但那并没有起作用。
最后,我尝试启用legacy peer deps并删除我的模块,然后重新安装。那也没有起作用,并在尝试本地运行时留下了这个错误:
./node_modules/aws-amplify-react/lib-esm/Amplify-UI/Amplify-UI-Components-React.js Attempted import error: 'a' is not exported from '@aws-amplify/ui' (imported as 'AmplifyUI').
我也无法解决那个问题。所以我再次删除了模块,重新运行npm install,现在我回到了最初的错误。

更新从React v16到v18后,仍然遇到相同的问题。 - Fazle Rabbi Ador
1个回答

7
我有完全相同的问题,但使用--legacy-peer-deps应该可以解决。你可能只是在本地运行它,而不是在Amplify中构建应用程序时运行它。
前往AWS Amplify中的应用程序,并从侧边栏菜单中选择“构建设置”。接下来只需替换。
preBuild:
 commands:
   - npm ci

使用:

preBuild:
 commands:
   - npm ci --legacy-peer-deps

这个方法至少对我有用,希望能对你有所帮助!

文档:https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html


你在哪里添加了这个命令? - Zeeshan Ahmad Khalil
@ZeeshanAhmadKhalil 如果你遇到和OP一样的问题,评论中已经解释了。进入你的应用程序的AWS Amplify,从侧边栏菜单中选择构建设置,并进行上述更改。 - undefined
@ZeeshanAhmadKhalil 如果你遇到和 OP 一样的问题,评论中也有解释。进入你的应用程序的 AWS Amplify,从侧边栏菜单中选择构建设置,并进行上述更改。 - Sadmiral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接