next.config.js: Custom Webpack Config | Next.js
Before continuing to add custom webpack configuration to your application make sure Next.js doesn't already support your use-case: Some commonly asked for features are available as plugins: In order to extend our usage of webpack, you can define a function that extends its config inside next.config.js, like so: The webpack function is executed twice, once for the server and once for the client.
https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config