Toastify React,尝试使用时出错



每当我尝试导入tosify到react:

使用如下:

import {ToastContainer, toast} from 'react-toastify';进口的react-toastify/dist/ReactToastify.css;

我得到以下错误:

编译失败。

。/node_modules/react-toastify/dist/react-toastify.esm.mjs不能从非EcmaScript模块中导入命名的导出'cloneElement'(只有默认的导出可用)

不确定解决方案是什么,花了很多时间试图找到解决方案,但我也无法,如果还有什么需要让我知道,但当tosify导入不在那里的网站工作良好。

尝试更改toastify的旧版本。手动将其添加到包中。Json文件"react-toastify"; "^8.1.0",然后运行NPM I,然后再次运行NPM start。

如本bug报告所述,这似乎是react-toastify与较旧版本的react-scripts一起使用时出现的问题。

你的选择是:

  • 升级到最新版本的react-scripts(版本5或以上)
  • react-toastify降级为9.0.3版本,这似乎仍然适用于react-scripts的旧版本

你只需要做这个

npm i react-toastify@9.0.3

似乎是由于节点版本的原因出现了某种错误。你能尝试用最新的节点版本复制这个吗?不要忘记删除node_modules和yarn。锁| package-lock.json

我通过更新我的节点版本和react版本来解决,

npm install——save react@latestNPM install -g NPM -check-updatesNPM审计修复——force

我在React 18.2.0中遇到了同样的问题,这是通过:

#upgrading to the latest version of react-scripts
npm install react-scripts@5.0.1  # OR npm install --save react-scripts@latest
#if you encounter errors after upgrading.
rm -r node_modules
npm install

我有同样的问题,我试图更新我的纱线,和反应版本,但问题仍然存在。最好的方法是将toastify版本降级到8.0.0,它就可以工作了。

  • 使用这个命令:

    npm i react-toastify@8.0.0
    
  • 或:如果您使用的是纱线

    yarn add react-toastify@8.0.0
    

最新更新