What is next config mjs
Next Config MJS Explained: Everything You Need to Know
Next.js has grow a staple for developers aiming to build fast, scalable React applications. A crucial part of optimizing and customizing these applications lies in understanding and effectively using the next.config.mjs file. This configuration file, typically named next.config.mjs and located in the root directory of your project, is a cornerstone for tailoring your Next.js app to meet specific needs.
Unlike traditional configuration files, next.config.mjs supports ES modules (ESM), allowing for a more modern and flexible approach to configuring both the Next.js server and assemble phases. It's important to letter that this file is not included in the browser construct, emphasizing its role in server and build-time configurations only.
This blog will delve into the intricacies of next.config.mjs, from basic configuration options and environment variables to advanced customization and optimization techniques.
Configuration Options and Environment Variables
At its core, next.config.mjs can be structured using the module.exports syntax or by exporting a function that returns a configuration object. This flexibility allows d
Request configuration
Configuration properties that you use across your Next.js app can be set per request.
Server & Client Components
Depending on if you handle internationalization in Server- or Client Components, the configuration from or will be applied respectively.
&
can be used to provide configuration for server-only code, i.e. Server Components, Server Actions & friends. The configuration is provided via the function and provides a parameter in case you’re using locale-based routing.
The configuration object is created once for each request by internally using React’s . The first component to use internationalization will call the function defined with .
Since this function is executed during the Server Components render pass, you can call functions like and to return configuration that is request-specific.
can be used to provide configuration for Client Components.
These props are inherited if you’re rendering from a Server Component:
If you don’t want to inherit some of these props, e.g. because you’re selective about how you use internationalization in Server & Client Components, you can opt-
Next.js 15 config
The next chapter will be about announcement that Next.js 15 now supports typescript for Next.js configuration file 🤩
After that, we will learn about config phases, what they are and how to use them
Finally we will tweak the react strict mode, adjust the powered by header and add redirects to our configuration
next.config.ts
The Next.js "next.config.ts" issue #5318 is from 2018, so a bit more than 6 years later the Next.js team announced in the Next.js 15 blog post that they added "Support for next.config.ts" 🤩
If you use CNA like we did on one of the previous page, then you will get a next.config.ts, but if you already have an existing project then you can rename your next.config**.js** or next.config**.mjs** to next.config**.ts** and it will just work (NO other steps required)
If the next.config.ts or the files options in your ESLint configuration and remove the .js or .
next.config.ts does NOT support "ESM only" packages
The Next.js team explains the problem well in the documentation, here is a quote from it:
Module resolution in next.config.ts is currently limited to CommonJS. This may cause incomp
![]()
next.config.js Options
Next.js can be configured through a file in the root of your project directory (for example, by ).
is a regular Node.js module, not a JSON file. It gets used by the Next.js server and build phases, and it's not included in the browser build.
If you need ECMAScript modules, you can use :
You can also use a function:
Since Next.js 12.1.0, you can use an async function:
is the current context in which the configuration is loaded. You can see the available phases. Phases can be imported from :
The commented lines are the place where you can put the configs allowed by , which are defined in this file.
However, none of the configs are required, and it's not necessary to understand what each config does. Instead, search for the features you need to enable or modify in this section and they will show you what to do.
js version. will not be parsed by Webpack, Babel or TypeScript.
This page documents all the available configuration options:
Failed to load next.config.mjs
About two hours ago, I successfully deployed version 37 of my project. After that, I continued working and added a new function to the app, which created version 38. However, when I attempted to deploy this new version, I suddenly started encountering a build error.
The build log shows the following message:
I’m not sure what changed exactly, as the only modification I made was adding a new function in my app’s logic—not in the configuration file. Everything was working fine before that.
To troubleshoot, I deleted version 38 and tried to redeploy version 37, which had previously deployed successfully. However, now version 37 is also failing with the exact same error, even though I hadn’t modified anything in that version.
I’d really appreciate any help figuring out what might be going on here or how I can fix it.
https://v0.dev/chat/fork-of-funcional-mas-reciente-24-mar-25-pero-no-h-7Wf26u4eY65
I also noticed something that might be related to the issue.
The error message refers to a file named , but in my project, I’ve always used the standard file (with the extension, not ). I haven’t created or renamed any config file recentl