38

(node:13176) [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:13176) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option

enter image description here

rofrol
  • 14,438
  • 7
  • 79
  • 77
Sagar Nilgar
  • 389
  • 1
  • 3
  • 4

9 Answers9

12

In file: node_modules/react-scripts/config/webpackDevServer.config.js

like this

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

change to

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;
},
Anton
  • 167
  • 1
  • 7
  • 12
    It is not advised to change files in the node_modules folder. Any changes there will be destroyed once dependencies are installed which should be happening on a production build or CI to ensure your deps are "clean" If, and I stress **IF** there is a need to update node_modules dependencies then the safer solution is to use patch-package. But this should be as limited as possible and when no other solution is possible, the better and longer term solution is to either open a PR with the changes or jump over to an existing PR and subscribe to it to get updates when it is merged. – Dav Jan 04 '23 at 00:40
  • Remember, Never EVER change anything in node_modules folder! Its equivalent of changing something in the generated bundle of js/css. – Sebastian Voráč MSc. May 28 '23 at 07:19
  • I agree with other comments that you should not edit files in the node_modules folder. Instead you can use the "eject" script as the "react-scripts" package is no longer maintained and then edit this file in your own project. – Michał D Jul 05 '23 at 12:01
11

I had the same issue. The problem was with the setupProxy.js.

If your setupProxy.js files look like below

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

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

Change it as below

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

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

As in the error, you have quoted, onAfterSetupMiddleware and onBeforeSetupMiddleware have been deprecated therefore the proxy method is not working as expected. So when you start the server with npm start the browser tries to redirect to the proxy URL. That's why you see as the react app is not loading.

6

This is a deprecation warning, meaning that you need to start using the newly suggested way of configuring your middleware. Instead of onBeforeSetupMiddleware and onAfterSetupMiddleware, use the setupMiddlewares-property.

The documentation itself can be found on Webpack's website (in this case here: https://webpack.js.org/configuration/dev-server/#devserversetupmiddlewares)

The configuration will look something like this (coming from onBeforeSetupMiddleware and onAfterSetupMiddleware):

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

    return middlewares
},

Another useful way of fixing this issue is by looking at the code others have written to fix this issue. I found this example to be helpful: https://github.com/facebook/docusaurus/pull/6168

Yentl VD
  • 61
  • 1
  • 5
3

I had the same error.

clip of error message

An authentication middleware, in my case AWS Amplify Auth, was changed incorrectly and my webpack config yielded an error. The fix for me was to re-install clean then debug the error.

So delete package-lock.json, delete the node-modules folder, and reinstall with "npm i".

Although this does not get to the root of the issue it should work to at least bring up the Dev Server to view any errors.

KrisCodes2
  • 73
  • 7
1

It's just a deprecation warning from one of the libraries you used within the app, have you tried to open http://localhost:3000 on your local? it is supposed to be running fine

sushitrash
  • 174
  • 1
  • 8
1

I had a similar behaviour. Running the react scripts and then being stuck at the "Starting the development server...". Although it run via "react-app-rewired" I guess this is some general thing with react-scripts.

I updated an existing project from react-scripts 4.0.3 to 5. That was my cause. I tried a lot of things but unfortunately nothing helped. I could not move forward and reverted the upgrade to the v5 of react-scripts.

Maybe this helps someone.

Aleks
  • 101
  • 1
  • 12
0

As one of the top answers mentioned the problem is setupProxy.js but in my case when I added this file, it prevented the development server to start so I didn't have any clues how this file is causing the problem. I wrapped app.use() in a try and catch block and the problem was solved.

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);
 }
}
csgeek
  • 711
  • 6
  • 15
sam
  • 31
  • 8
-1

What fixed it for me was running npm i webpack-dev-middleware.

-1

Tried changing the names in webpackDevServer.config.js it worked for me.

  1. Open node_modules folder.
  2. Search for webpackDevServer.config.js.
  3. Open the js file and edit it.
cursorrux
  • 1,382
  • 4
  • 9
  • 20