Nuxt.js: process is not defined

Created on 14 May 2019  路  15Comments  路  Source: nuxt/nuxt.js

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

This bug report is available on Nuxt community (#c9208)
available soon bug-report

Most helpful comment

@pi0
I have ^2.10.2 and still reproduce

All 15 comments

can confirm just updated my dependencies, including nuxt to 2.7, and this killed my app

Could one of you provide a minimal reproduction repo or codesandbox please?

ci-info is not a dependency of @nuxtjs/dotenv so the issue may be originating from somewhere else or import/require a Node.js lib in application code. Can you please share result of yarn why ci-info? (only std-env should be listed) (Sharing nuxt.config.js would be useful as well)

I have the same bug.

小薪懈屑芯泻 褝泻褉邪薪邪 2019-05-14 胁 17 54 47

I use (source):

const envPath = path.resolve(__dirname, 'config', '.env')
require('dotenv').config({ 'path': envPath })

And also:

/**
   * Environment variables
   */
  'env': {
    'GOOGLE_MAPS_KEY': process.env.GOOGLE_MAPS_KEY,
  },

in my local nuxt.config.ts.

@sobolevn Could you chat with me on discord so that we can get response timely ?

@clarkdo sure. Texted you

I have the same issue.

output of npm list ci-info:

+-- @nuxtjs/[email protected]
| -- [email protected]
| -- [email protected]
| -- [email protected]
| -- [email protected] deduped
-- [email protected]
+-- @nuxt/[email protected]
| +-- @nuxt/[email protected]
| | -- [email protected]
| | -- [email protected] deduped
| -- [email protected]
| -- [email protected]
+-- @nuxt/[email protected]
| -- [email protected]
| -- [email protected] deduped
-- @nuxt/[email protected]
+-- [email protected]
| -- [email protected] deduped
-- [email protected]
-- [email protected]
-- [email protected] deduped

*Edit: I tried to fix formatting but cant make it any better, so I made a screenshot too
1

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

fixed in v2.7.1

@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...

@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!

Could someone update the docs on this? Doesn't seem to work as intended on [email protected].

process is undefined.

@thomasdavis Do you have the line require('dotenv').config() at the top of your nuxt.config.js? That has solved it for me.

@thomasdavis try require('dotenv').config() first. If that doesnt work, try

const env = require('dotenv');
env.config();

This code has worked when require('dotenv').config() doesnt.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattdharmon picture mattdharmon  路  3Comments

jaredreich picture jaredreich  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

danieloprado picture danieloprado  路  3Comments

msudgh picture msudgh  路  3Comments