nuxt: process is not defined

Version

v2.7.0

Reproduction link

https://github.com/nuxt/nuxt.js/releases/tag/v2.7.0

Steps to reproduce

  • npm up to upgrade to nuxt 2.7.0
  • have "@nuxtjs/dotenv": "^1.3.0", as dep in your project
  • have require('dotenv').config(); in your nuxt.config.js file

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 unknown

Screenshot from me: Screenshot 2019-05-14 at 6 30 52 PM

<div align="right">This bug report is available on Nuxt community (#c9208)</div>

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@pi0 I have ^2.10.2 and still reproduce

@pi0 I have ^2.10.2 and still reproduce

Same, I’m running 2.11 and still can’t access process

Okay, I think I was misunderstanding a few things. NODE_ENV defaults 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.env but does not implicitly propagate that access to the rest of the code.

So in order to have access to something like process.env.NODE_ENV from my components, I needed to do a few things.

First, in nuxt.config.js, you need to propagate your process.env.whatever values under an env key in your module.exports so that your components can access them too. In this case, I want NODE_ENV.

nuxt.config.js

module.exports = {
  ...
  env: {
    NODE_ENV: process.env.NODE_ENV
  },
  ...
}

Now, when explicitly attempting to access process.env.NODE_ENV in any of my components, I get production or development depending on the command I run (npm dev produces “development”, npm start produces “production”)

YourComponent.vue

...

mounted () {
  console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`) // process.env.NODE_ENV: development
}

...

Something that was confusing me was that I couldn’t access process.env.NODE_ENV in the debugger in the browser. I would set a debugger in the same methods/hooks I’m logging process.env.NODE_ENV in as an example, and I get undefined for process.env.NODE_ENV and an empty object {} for process.env. Before that, I was getting things like process 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_ENV or use cross-env or anything for my scripts in package.json).

Hopefully this helps someone!

Through the communication with @sobolevn , the issue is due to hoisted consola version, if you’re having this issue, please install consola 2.x manually by:

$ npm i consola@^2.6.1

# or

$ yarn add consola@^2.6.1

We’ll propose a fix ASAP

Issue raised again on Nuxt 3.1.2 image

@clarkdo sure. Texted you