Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm run build --mode [.env.mode] not working as expected

Tags:

What I've done so far:

I've been trying to setup multiple build modes like staging, testing, production and development based on NODE_ENV=production. So I'm keeping the respective files in the root of the project folder like:

  • .env.production
  • .env.staging
  • .env.testing
  • .env.development

Now, all these files are having

NODE_ENV=production VUE_APP_ENV=<mode> 

The document that I followed clearly states that,

vue-cli-service build --mode staging builds a production app in staging mode, using .env, .env.staging and .env.staging.local if they are present.

Problem:

As expected, running the command npm run build --mode staging is to give a production build with variable as listed in the .env.staging file. However, production variables are loaded instead of staging.

Ref:

  • https://cli.vuejs.org/guide/mode-and-env.html#example-staging-mode
  • https://forum.vuejs.org/t/how-to-build-production-app-with-varying-config/29708
like image 620
Anoop Thiruonam Avatar asked Jun 09 '18 09:06

Anoop Thiruonam


People also ask

What does NPM run build--mode staging do?

As expected, running the command npm run build --mode staging is to give a production build with variable as listed in the .env.staging file. However, production variables are loaded instead of staging.

Does node_env need to be explicitly set?

This issue actually seems resolved for me as I misunderstood the documentation. NODE_ENV needs to be set explicitly and it is it works, even if using "staging":"vue-cli-service build --mode staging" in package.json.

Is there a way to get production-oriented build in staging env?

In other words: all arguments before -- are interpreted as arguments for npm itself, not the script you are running. Sorry, something went wrong. @LinusBorg There is no way that you get a production oriented build in a staging env. I added the npm commands just to demonstrate. Sorry, something went wrong.

Is it possible to set node_ENV to staging?

Setting NODE_ENV to staging makes no sense whatshowever since staging is not known by any packages, including React. Your staging environment should be as close as possible to production, meaning same build, same NODE_ENV.


2 Answers

You need to use the following command

npm run build -- --mode staging

All arguments before -- are considered npm arguments and arguments after -- are passed to vue-cli-service

like image 84
ZaidRehman Avatar answered Sep 17 '22 01:09

ZaidRehman


I was having the same problem, I figured out my problem was from using a beta version (3.0.0-beta.9) of @vue/cli-service so changing it to the rc version (3.0.0-rc.3) worked. So in my package.json under devDependencies I changed it to "@vue/cli-service": "^3.0.0-rc.3"

like image 38
Jonathon Gardner Avatar answered Sep 17 '22 01:09

Jonathon Gardner