nuxt-community/separate-env-module

How to use with Express?

Closed this issue · 4 comments

$ cat /nuxt/server/index.js
console.log(process.env.ONLY_SERVER); // undefined
console.log(process.env.ONLY_CLIENT); // undefined

Why aren't these parsed/available?

Thanks. :)

Please provide more context:

  • Where/How is /nuxt/server/index.js called?

@manniL Sorry for the lack of details.

I'm using Nuxtjs + Express, and this is nuxt.config.js as example:

{
  env: {
    server: {
      ONLY_SERVER: 'yup',
      DIFFERENT_ON_BOTH: 'server'
    },
    client: {
      ONLY_CLIENT: 'okay',
      DIFFERENT_ON_BOTH: 'client'
    },
    normalEnvVariableThatWillBeAvailableEverywhere: 'Hi'
  }
}

package.json

..
  "scripts": {
    "build": "nuxt build",
    "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch api",
    "generate": "nuxt generate",
    "lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
    "precommit": "npm run lint",
    "start": "cross-env NODE_ENV=production node server/index.js"
  },
..

I'm trying to access process.env.ONLY_SERVER in server/index.js, but as you can see it returns undefined.

This module only works inside Nuxt and not in layers above Nuxt (like express).

Try to use Nuxt + Express via serverMiddleware or, as I'd suggest, separate both parts.

PS: Blog post following to exactly that topic soon™️

@manniL OK, thanks for the help and will keep an eye on your blog post.