nuxt: process is not defined
Version
Reproduction link
https://github.com/nuxt/nuxt.js/releases/tag/v2.7.0
Steps to reproduce
npm upto upgrade to nuxt 2.7.0- have
"@nuxtjs/dotenv": "^1.3.0",as dep in your project - have
require('dotenv').config();in yournuxt.config.jsfile
try to use process.env variables in your nuxt.config.js, and it’ll throw a error
What is expected ?
Ability to access .env variables inside the nuxt app
What is actually happening?
Nuxt app breaks down squawking about the process error
Additional comments?
Screenshot from @cannap on Discord

Screenshot from me:

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 17 (5 by maintainers)
@pi0 I have
^2.10.2and still reproduceOkay, I think I was misunderstanding a few things.
NODE_ENVdefaults are indeed set (see this commit)My understanding in a nutshell (correct me if I’m wrong) is that nuxt.config.js has access to
process.envbut does not implicitly propagate that access to the rest of the code.So in order to have access to something like
process.env.NODE_ENVfrom my components, I needed to do a few things.First, in nuxt.config.js, you need to propagate your
process.env.whatevervalues under anenvkey in yourmodule.exportsso that your components can access them too. In this case, I wantNODE_ENV.nuxt.config.js
Now, when explicitly attempting to access
process.env.NODE_ENVin any of my components, I getproductionordevelopmentdepending on the command I run (npm devproduces “development”,npm startproduces “production”)YourComponent.vue
Something that was confusing me was that I couldn’t access
process.env.NODE_ENVin the debugger in the browser. I would set a debugger in the same methods/hooks I’m loggingprocess.env.NODE_ENVin as an example, and I getundefinedforprocess.env.NODE_ENVand an empty object{}forprocess.env. Before that, I was getting things likeprocess is undefined.Despite all that weirdness, I can now access env vars, including NODE_ENV vars that are generated by default (I don’t set
NODE_ENVor usecross-envor anything for my scripts in package.json).Hopefully this helps someone!
Through the communication with @sobolevn , the issue is due to hoisted
consolaversion, if you’re having this issue, please installconsola 2.xmanually by:We’ll propose a fix ASAP
Issue raised again on Nuxt 3.1.2
@clarkdo sure. Texted you