将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上运行相同的命令,则不会出现此错误。如何为Windows修复此问题?