启动服务器后无法加载React应用程序

38

(node:13176) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware'选项已过时,请使用'setupMiddlewares'选项。(使用node --trace-deprecation ...显示警告的创建位置) (node:13176) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware'选项已过时,请使用'setupMiddlewares'选项。

输入图像描述


FYI:已为此创建了一个问题:https://github.com/facebook/create-react-app/issues/11860 - Monica Granbois
这只是一个警告,不应该阻止程序运行。你尝试在浏览器中加载页面了吗? - Code-Apprentice
9个回答

12
在文件 node_modules/react-scripts/config/webpackDevServer.config.js 中
就像这样
onBeforeSetupMiddleware(devServer) {
    // Keep `evalSourceMapMiddleware`
    // middlewares before `redirectServedPath` otherwise will not have any effect
    // This lets us fetch source contents from webpack for the error overlay
    devServer.app.use(evalSourceMapMiddleware(devServer));

    if (fs.existsSync(paths.proxySetup)) {
    // This registers user provided middleware for proxy reasons
    require(paths.proxySetup)(devServer.app);
    }
},
onAfterSetupMiddleware(devServer) {
    // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
    devServer.app.use(redirectServedPath(paths.publicUrlOrPath));

    // This service worker file is effectively a 'no-op' that will reset any
    // previous service worker registered for the same host:port combination.
    // We do this in development to avoid hitting the production cache if
    // it used the same host and port.
    // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
    devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
},

更改为

setupMiddlewares: (middlewares, devServer) => {
    if (!devServer) {
        throw new Error('webpack-dev-server is not defined')
    }

    if (fs.existsSync(paths.proxySetup)) {
        require(paths.proxySetup)(devServer.app)
    }

    middlewares.push(
        evalSourceMapMiddleware(devServer),
        redirectServedPath(paths.publicUrlOrPath),
        noopServiceWorkerMiddleware(paths.publicUrlOrPath)
    )

    return middlewares;
},

15
不建议更改node_modules文件夹中的文件。任何更改都将在安装依赖项时被销毁,这应该在生产构建或CI上发生,以确保您的依赖项是“干净”的。如果有必要更新node_modules依赖项,则更安全的解决方案是使用patch-package。但这应该尽可能地受限制,并且当没有其他解决方案时,更好和长期的解决方案是要么打开一个带有更改的PR,要么跳转到现有的PR并订阅它以获取合并后的更新。 - Dav
记住,绝对不要更改node_modules文件夹中的任何内容!这相当于在生成的js/css捆绑包中更改内容。 - Sebastian Voráč
我同意其他评论,你不应该在node_modules文件夹中编辑文件。相反,你可以使用"eject"脚本,因为"react-scripts"包已经不再维护,然后在你自己的项目中编辑这个文件。 - Michał D

12

我遇到了同样的问题。问题出在setupProxy.js文件上。

如果你的setupProxy.js文件看起来像下面这样:

const proxy = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(proxy('/proxypath', { target: '<target path>' }));
};

请将其更改为以下内容

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(createProxyMiddleware('/proxypath', { target: '<target path>' }));
};

如你所引用的错误信息,onAfterSetupMiddleware和onBeforeSetupMiddleware已被弃用,因此代理方法无法正常工作。所以当您使用npm start启动服务器时,浏览器会尝试重定向到代理URL。这就是为什么你看到React应用程序未能加载。


我尝试过这个,但对我没有起作用。 - bheklilr
确保在createProxymiddleware()中将目标设置为您的API URL,这是我遇到此错误的原因之一。 - Marty Farce

6

这是一条废弃警告,意味着您需要开始使用新推荐的中间件配置方式。不再使用onBeforeSetupMiddlewareonAfterSetupMiddleware,而是使用setupMiddlewares属性。

文档本身可以在Webpack的网站上找到(在此情况下在这里:https://webpack.js.org/configuration/dev-server/#devserversetupmiddlewares

配置将类似于以下内容(来自onBeforeSetupMiddlewareonAfterSetupMiddleware):

setupMiddlewares: (middlewares, devServer) => {
    middleware1(devServer.app)
    middleware2(devServer.app)

    return middlewares
},

解决这个问题的另一种有用方法是查看其他人编写的代码来解决此问题。我发现这个示例很有帮助:https://github.com/facebook/docusaurus/pull/6168


3

我遇到了同样的错误。

错误信息截图

在我的情况下,一个身份验证中间件(AWS Amplify Auth)被错误地更改,导致我的webpack配置出现错误。 对我来说,解决方法是重新安装清理后再调试错误。

因此,请删除 package-lock.json 文件和 node-modules 文件夹,并使用 "npm i" 重新安装。

虽然这并没有解决问题的根本原因,但可以至少启动开发服务器以查看任何错误。


1

我遇到了类似的问题。运行React脚本后,卡在“启动开发服务器...”这一步。虽然通过“react-app-rewired”可以运行,但我想这可能是React-scripts的一般性问题。

我将一个现有项目从React-scripts 4.0.3更新到5。这就是我的原因。我尝试了很多方法,但不幸的是没有什么帮助。我无法继续前进,只能将React-scripts的升级还原到v5。

也许这可以帮助某些人。


你是说将 react-scripts 更新到 4.0.3 到 5 解决了问题(即消除了警告)吗?还是这导致了问题? - Woodchuck
1
那是我的原因。 - Aleks
是的,我也遇到了同样的问题。 - Dulya Perera

1

这只是你在应用程序中使用的一个库发出的弃用警告,你尝试在本地打开http://localhost:3000了吗?它应该能正常运行。


0
正如其中一位顶级答案所提到的问题是setupProxy.js,但在我的情况下,当我添加了这个文件时,它阻止了开发服务器的启动,因此我没有任何线索,这个文件是如何引起问题的。我将app.use()包装在try和catch块中,问题得到了解决。
const proxy = require('http-proxy-middleware');

module.exports = function(app) {
 try{
    app.use(
        '/api',
        proxy({
          target: 'http://localhost:3001',
          changeOrigin: true
        })
      )
 }
 catch(err){
    console.log(err.message);
 }
}

-1

尝试更改webpackDevServer.config.js中的名称,这对我有效。

  1. 打开node_modules文件夹。
  2. 搜索webpackDevServer.config.js。
  3. 打开js文件并进行编辑。

-1

对我有用的解决方法是运行 npm i webpack-dev-middleware


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接