Nuxt Tip: Use Environment-Specific Configurations
Michael Hoffmann
@mokkapps
It sometimes happens that you need to have different configurations for different environments in your Nuxt.js application. In this article, I'll show you how to use environment-specific Nuxt configurations.
Let's start with a simple nuxt.config.ts
file:
export default defineNuxtConfig({
app: {
head: {
title: 'My App'
}
}
})
For demonstration purposes, let's assume that we want to have a different title in development and production. You might be tempted to use an environment variable like this:
export default defineNuxtConfig({
app: {
head: {
title: process.env.NODE_ENV === 'development' ? 'My App (Dev)' : 'My App'
}
}
})
This approach is not recommended by Nuxt, instead, you should use the fully typed, per-environment overrides like $production
, $development
or $test
in your nuxt.config
file. Here's how you can do it:
export default defineNuxtConfig({
app: {
head: {
title: 'My App'
}
},
$development: {
app: {
head: {
title: 'My App (Dev)'
}
}
}
})
The $development
key is a special key that allows you to define a Nuxt configuration for the development environment. This configuration is merged with the default configuration when running in development mode. This mechanism is powered by c12, a smart configuration loader available in the UnJS ecosystem.
$meta
key to provide metadata that you or the consumers of your layer might use.If you liked this Vue tip, follow me on BlueSky to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter :
Vue Tip: Share Styling Using Wrapper Components
Vue Tip: Validate Props in Script Setup With TypeScript