Skip to content

DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated #12035

Description

@tunaphamit

I got a warning when npm start.
Please help me solve the problem.

(node:1681) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use node --trace-deprecation ... to show where the warning was created)
(node:1681) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.

Activity

  1. linzhipeng98 commented on Feb 10, 2022

    @linzhipeng98
  2. Cyebukayire commented on Mar 10, 2022

    @Cyebukayire

    I'm having the same warning too
    Any solution to this?

  3. johnnychengchj123 commented on Mar 10, 2022

    @johnnychengchj123

    me too

    I'm having the same warning too Any solution to this?

  4. imransilvake commented on Mar 10, 2022

    @imransilvake

    same issue

  5. iiaan commented on Mar 10, 2022

    @iiaan

    i have the same problem

  6. YousefAlbarqawi commented on Mar 11, 2022

    @YousefAlbarqawi

    I thought i was the only one. but let me ask something is this error has to do with the ERR_CONNECTION_REFUSED error?

  7. khizar-hayyat-72 commented on Mar 15, 2022

    @khizar-hayyat-72

    Same Error here.

  8. gars150387 commented on Mar 17, 2022

    @gars150387

    I am getting the same error:

    "(node:9112) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
    (Use node --trace-deprecation ... to show where the warning was created)
    (node:9112) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option."

  9. oxicot commented on Mar 17, 2022

    @oxicot

    The same thing here after upgrade to ..... "react-scripts": "^5.0.0" (Node 16.*)


    $ npm start

    test@0.1.0 start
    react-scripts start

    (node:21944) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
    (Use node --trace-deprecation ... to show where the warning was created)

    (node:21944) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
    Starting the development server...

    Compiled successfully!

    webpack 5.70.0 compiled successfully in 18347 ms

  10. nspool commented on Mar 25, 2022

    @nspool

    Is this a duplicate of #11860 ?

  11. gars150387 commented on Mar 25, 2022

    @gars150387

    This warning disappeared using the repair option in the node.js file. It worked for me


    I got a warning when npm start. Please help me solve the problem.

    (node:1681) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option. (Use node --trace-deprecation ... to show where the warning was created) (node:1681) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.

  12. HaseebullahMemon commented on Mar 26, 2022

    @HaseebullahMemon

    i am facing node(8868) type of error. Please help me solve the problem
    (node:8868) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE]
    DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
    (Use node --trace-deprecation ... to show where the warning was created)
    (node:8868) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.

  13. SohelTanbir commented on Mar 27, 2022

    @SohelTanbir

    I got also the same warning!
    please any solution available ?
    (node:11648) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option

  14. anveshiano commented on Mar 27, 2022

    @anveshiano

    I am facing the same issue

  15. 83 remaining items

  16. anhtuank7c commented on Oct 4, 2023

    @anhtuank7c

    I did npm remove react-scripts --force and then npm add -D react-scripts --force
    🎉 it works.

  17. GCE333 commented on Jan 5, 2024

    @GCE333

    It seems like Create React App is dead and this issue will never be fixed. The best solution is to use Vite instead.

  18. JEANTAYLON commented on Mar 1, 2024

    @JEANTAYLON

    removing the following code from node_modules\react-scripts\config\webpackDevServer.config.js made the warning go away

    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));
    },
  19. JohnMaxwellDistinti commented on Mar 3, 2024

    @JohnMaxwellDistinti

    Hi All,

    Please refer to this article for an alternative snippet to "onBeforeSetupMiddleware" and "onAfterSetupMiddleware" within
    node_modules/react-scripts/config/webpackDevServer.config.js:

    https://discuss.codecademy.com/t/react-app-error/779264/2

    This has fixed the error for me! 👍

  20. ParuchuriAmitha commented on Jun 6, 2024

    @ParuchuriAmitha

    I used have same issue i rectified it by below solution

    Step-1 Go to this file path : node_modules\react-scripts\config\webpackDevServer.config.js
    Step - 2 Replace below code

    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.
    // #2272 (comment)
    devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
    },

    Step - 3 With the below code

    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;
    }

  21. ruthyankson commented on Jun 15, 2024

    @ruthyankson

    This was the error I had

    (node:20104) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupM
    Compiled successfully!

    After a number of searches and trials, I solved it by visiting

    • node_modules/react-scripts/config/webpackDevServer.config.js

    And replaced this deprecated code:

      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));
        },

    With this current one:

      setupMiddlewares: function(middlewares, devServer) {
            // Middleware setup code
            devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
            devServer.app.use(redirectServedPath(paths.publicUrlOrPath));
            return middlewares;
        },
  22. kitsanapong commented on Jun 28, 2024

    @kitsanapong

    I used Cargo(https://craco.js.org/docs/configuration/devserver/) to overwrite Weppack configs.

    cargo.config.js

    module.exports = {
      webpack: {
        devServer: {
          setupMiddlewares: function(middlewares, devServer) {
            if (fs.existsSync(paths.proxySetup)) {
              require(paths.proxySetup)(devServer.app)
            }
            
            middlewares.push(
                evalSourceMapMiddleware(devServer),
                redirectServedPath(paths.publicUrlOrPath),
                noopServiceWorkerMiddleware(paths.publicUrlOrPath)
            )
            
            return middlewares;
          },
        }
      },
      devServer: (devServerConfig, { env, paths, proxy, allowedHost }) => {
        delete devServerConfig.onBeforeSetupMiddleware
        delete devServerConfig.onAfterSetupMiddleware
        return devServerConfig
      },
    }
    
  23. fabiopasilva1 commented on Sep 8, 2024

    @fabiopasilva1

    Finalmente consertei isso. Se você estiver usando o Yarn, basta executar o Yarn eject, então você deverá encontrar o arquivo '/config/webpackDevServer.config.js'. Em vez de onBeforeSetupMiddleware e onAfterSetupMiddleware, use a propriedade setupMiddlewares. Como antes:

    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));
        },
    

    depois:

    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;
        },
    

    mas não funciona quando você hospeda o servidor porque você não pode alterá-lo lá, pois isso deve ser feito nos módulos do nó

    Como editaremos o código no node-modules no pipeline de implantação

    It worked for me, thanks

  24. ananya-singh-afk commented on Oct 9, 2024

    @ananya-singh-afk

    facing same issue :
    (node:620) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
    (Use node --trace-deprecation ... to show where the warning was created)
    (node:620) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.

    any solutions to this?

  25. ananya-singh-afk commented on Oct 9, 2024

    @ananya-singh-afk

    My way of solving this was... npm run build, then npm install -g serve, npx serve -s build. I couldn't figure out the webpack shit!! and the problem lied with the npm start so instead I used npx serve -s build... hope it helps!

    great thanks!

  26. Soloctg commented on Jan 8, 2025

    @Soloctg

    just install webvital by
    npm install web-vitals

  27. Soloctg commented on Jan 8, 2025

    @Soloctg

    facing same issue : (node:620) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option. (Use node --trace-deprecation ... to show where the warning was created) (node:620) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.

    any solutions to this?

    npm install web-vitals

  28. ThiagoSzz commented on Jun 13, 2025

    @ThiagoSzz

    On my app I was able to solve this by creating a patch for react-scripts using patch-package:

    1. Install patch-package
    npm i patch-package
    1. Find the packages/react-scripts/config/getHttpsConfig.js and packages/react-scripts/config/webpackDevServer.config.js files under react-scripts in node_modules and replace their contents by the fix available at this PR opened in the create-react-app repository

    2. Create a patch for react-scripts

    npx patch-package react-scripts
    1. On package.json, call patch-package on postinstall script in order to apply the patch after each npm install
    "scripts": {
        "postinstall": "patch-package"
    }
    1. Delete node_modules and install it again
    npm i
  29. meysam4n commented on Jul 4, 2025

    @meysam4n

    Today I installed a fresh react app and got the same error in my first running :))

  30. ZoHere commented on Oct 14, 2025

    @ZoHere

    FIXED The issue by Restarting the PC seems like the issue was related to the WebPack and the Browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions