3

I am building a Nuxt application where I will have two environments staging and production, the local should also be considered as staging, now I need to create some commands and generate builds for production and staging, which will be deployed on two separate servers.

I have two questions

  1. The command
npm run generate

always generate a production build, I checked it using

console.log(process.env.NODE_ENV)

How can I generate a new build where the env should be something like staging?

  1. I want to create some .env files for holding some env related variables, but I am confused about how can I create multiple env files for multiple envs (staging and production).

I understand my question is a bit of research orientation, I spent days researching on the internet, but either the blogs are unrelated or confusing. I never really got what I was looking for, can someone point me into the right direction?

kissu
  • 40,416
  • 14
  • 65
  • 133
uneeb meer
  • 882
  • 1
  • 8
  • 21
  • `NODE_env` needs to be defined on the staging platform, same goes for the env files. Meaning that if you want some variable locally, set it in `.env`, if you want a specific variable for staging/production on Heroku, AWS or alike, set the env variable there (not in an `.env` but on some dashboard/settings tab). – kissu Dec 05 '21 at 19:49
  • If I'm not mistaken, `generate` uses the default `production` if nothing is passed. If you give it a specific value, either in `.env` or by prefixing `npm run generate`, it should be good! – kissu Dec 05 '21 at 19:50
  • @kissu I generated a build by defining NODE_ENV=staging in .env nothing changed still shows production when i create a build – uneeb meer Dec 05 '21 at 19:55
  • It's because the build will overwrite the env variable at build time. – kissu Dec 05 '21 at 22:44

2 Answers2

1

As told above, you can do
NODE_ENV="staging" npm run generate
while building the app, even tho this is not recommended and you should probably just use another env variable like ENV and make conditionals based on this one for your app.

If you want more details on how to use env variables, you can look at this answer.

Then, the way env variables work still applies to what I said about AWS/Heroku etc...

kissu
  • 40,416
  • 14
  • 65
  • 133
0

use nuxt --dotenv and pass the path to your env file like .env.production Example package.json:

{
  "scripts":{
    "prod":"NODE_ENV=production && nuxt --dotenv .env.production"
  }
}
ihor.eth
  • 2,207
  • 20
  • 27