The env property
Share environment variables between client and server.
-
Type:
Object
Nuxt lets you create environment variables client side, also to be shared from server side.
The env property defines environment variables that should be available on the client side. They can be assigned using server side environment variables, the dotenv module ones or similar.
env property with runtimeConfig properties : publicRuntimeConfig and privateRuntimeConfig.
Learn more with our tutorial about moving from @nuxtjs/dotenv to runtime config .
Make sure to read about process.env and process.env == {} below for better troubleshooting.
export default {
env: {
baseUrl: process.env.BASE_URL || 'http://localhost:3000'
}
}
This lets you create a baseUrl property that will be equal to the BASE_URL server side environment variable if available or defined. If not, baseUrl in client side will be equal to 'http://localhost:3000'. The server side variable BASE_URL is therefore copied to the client side via the env property in the nuxt.config.js. Alternatively, the other value is defined (http://localhost:3000 ).
Then, I can access my baseUrl variable in 2 ways:
-
Via
process.env.baseUrl. -
Via
context.env.baseUrl, see context API .
You can use the env property for giving a public token for example.
For the example above, we can use it to configure axios .
import axios from 'axios'
export default axios.create({
baseURL: process.env.baseUrl
})
Then, in your pages, you can import axios like this: import axios from '~/plugins/axios'
Automatic injection of environment variables
If you define environment variables starting with NUXT_ENV_ in the build phase (e.g. NUXT_ENV_COOL_WORD=freezing nuxt build or SET NUXT_ENV_COOL_WORD=freezing & nuxt build for the Windows console), they'll be automatically injected into the process environment. Be aware that they'll potentially take precedence over defined variables in your nuxt.config.js with the same name.
process.env == {}
Note that Nuxt uses webpack's definePlugin to define the environmental variable. This means that the actual process or process.env from Node.js is neither available nor defined. Each of the env properties defined in nuxt.config.js is individually mapped to process.env.xxxx and converted during compilation.
Meaning, console.log(process.env) will output {} but console.log(process.env.your_var) will still output your value. When webpack compiles your code, it replaces all instances of process.env.your_var with the value you've set it to, e.g.: env.test = 'testing123'. If you use process.env.test in your code somewhere, it is actually translated to 'testing123'.
before
if (process.env.test == 'testing123')
after
if ('testing123' == 'testing123')
serverMiddleware
As serverMiddleware is decoupled from the main Nuxt build, env variables defined in nuxt.config.js are not available there.
Clément Ollivier
Daniel Roe
Alex Hirzel
Ajeet Chaulagain
René Eschke
Sébastien Chopin
Nico Devs
Muhammad
Nazaré da Piedade
Naoki Hamada
Tom
Yann Aufray
Anthony Chu
Nuzhat Minhaz
Lucas Portet
Richard Schloss
Xanzhu
bpy
Antony Konstantinidis
Hibariya
Jose Seabra
Eze
Florian LEFEBVRE
Lucas
Julien SEIXAS
Hugo
Sylvain Marroufin
Spencer Cooley
Piotr Zatorski
Vladimir Semenov
Harry Allen
kazuya kawaguchi
Unai Mengual
Hyunseung Lee
Alexandre Chopin
pooya parsa
Nick Medrano
Mosaab Emam
Iljs Путлер Капут
Heitor Ramon Ribeiro
Nero
Yoon Han
Ikko Ashimine
FamCodings
Ayouli
F. Hinkelmann
felipesuri
Christophe Carvalho Vilas-Boas
Leoš Literák
Trizotti
Marcello Bachechi
Rodolphe
Thomas Underwood
Shek Evgeniy
Lukasz Formela
Hugo Torzuoli