reactjs - 升级 react-scripts 在运行 react-scripts start 时出错
问题描述
将 react-scripts 更新到版本3.4.0
并在 Windows 中运行react-scripts start
后,我收到此错误:
Must use import to load ES Module: C:\Users\myUser\Desktop\Projects\myproject\frontend\src\setupProxy.js
require() of ES modules is not supported.
require() of C:\Users\myUser\Desktop\Projects\myproject\frontend\src\setupProxy.js from C:\Users\myUser\Desktop\Projects\myproject\frontend\node_modules\react-scripts\config\webpackDevServer.config.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename setupProxy.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\myUser\Desktop\Projects\myproject\frontend\package.json.
这是文件setupProxy
:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
const proxyTarget = process.env.REACT_APP_API || 'http://localhost:8089';
console.log(`proxying /backend/api/* to ${proxyTarget}/backend/*`);
app.use(proxy('/backend/api', { target: proxyTarget}));
};
为什么我升级后会出现这个错误react-scripts
,并且它只显示在windows上,如果我在mac上运行相同的命令,那么我不会收到这个错误。我该如何解决这个问题?
解决方案
推荐阅读
- javascript - 执行两个异步调用时结果未定义
- c++ - new(std::nothrow) int[n] 抛出异常
- javascript - 是否可以在现场克隆 Vuejs 属性?
- java - 经典问题单车道桥问题的无饥饿解决方案
- sql-server - 收到错误“找不到对象'XXX',因为它不存在或您没有权限。” 在 SQL Server 中记录为 SA
- python - 什么是哈希表、哈希和键?
- java - Java - CSV 中的换行符转义
- javascript - Npm 续集安装
- java - Java openStream方法字符限制?
- javascript - CSS 变换或缩放?安装动画