首页 > 解决方案 > 升级 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上运行相同的命令,那么我不会收到这个错误。我该如何解决这个问题?

标签: reactjsreact-scripts

解决方案


推荐阅读