React 项目启动报如下错误:
通过查阅相关博客知道,报错原因为浏览器安装了react-devtools扩展程序导致的。
解决方法一、通过报错信息得知具体的报错地点,注释相关代码
从报错信息可以看到,报错是否node_modules/@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js
中发出,注释相关代码,如下
const safeThis = require('./utils/safeThis');
if (process.env.NODE_ENV !== 'production' && typeof safeThis !== 'undefined') {
// Only inject the runtime if it hasn't been injected
if (!safeThis.__reactRefreshInjected) {
const RefreshRuntime = require('react-refresh/runtime');
// Inject refresh runtime into global scope
// 将以下代码注释
// RefreshRuntime.injectIntoGlobalHook(safeThis);
// Mark the runtime as injected to prevent double-injection
safeThis.__reactRefreshInjected = true;
}
}
解决方法二、在chrome扩展程序禁用当前react-dev-tools,下载其他版本的react-dev-tools
如下图,暂时禁用当前react-dev-tools
:
重新下载 (https://www.crx4chrome.com/crx/3068/)
下载后直接改为.zip文件,拖到浏览器扩展程序处安装即可