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

Created on 9 Jun 2018  路  10Comments  路  Source: vuejs/vue-cli

Version

3.0.0-beta.16

Reproduction link

https://codesandbox.io/s/qlx254n09j

Steps to reproduce

  1. Create .env.staging file in the root
  2. Add key pairs NODE_ENV=production and VUE_APP_ENV=staging in the file
  3. Add script to package.json "staging":"vue-cli-service build --mode staging",
  4. Run npm run build --mode staging
  5. Run npm run staging

What is expected?

Case 1

npm run buid --mode staging

gives a production oriented build with staging variables

Case 2

npm run staging

gives a production oriented build with staging variables

What is actually happening?

Case 1

It gives a production oriented build with production variables

Case 2

It gives a development oriented build with staging variables


I need this to deploy this to Heroku. CD/CI. I'm using postinstall script to build production, dev or staging servers based on the APP_VUE_ENV variable. Please help me. I'm super excited with Vue so far!

Reference:

https://cli.vuejs.org/guide/mode-and-env.html#example-staging-mode
https://github.com/vuejs/vue-cli/issues/948
https://forum.vuejs.org/t/how-to-build-production-app-with-varying-config/29708

Most helpful comment

This is related to how npm passes arguments to scripts when using npm run

# passes the `mode` argument to npm
npm run build --mode staging

#passes the `mode` argument to vue-cli service`
npm run build -- --mode staging

In other words: all arguments before -- are interpreted as arguments for npm itself, not the script you are running.

All 10 comments

This is related to how npm passes arguments to scripts when using npm run

# passes the `mode` argument to npm
npm run build --mode staging

#passes the `mode` argument to vue-cli service`
npm run build -- --mode staging

In other words: all arguments before -- are interpreted as arguments for npm itself, not the script you are running.

@LinusBorg There is no way that you get a production oriented build in a staging env. I added the npm commands just to demonstrate.

Then please provide an actual reproduction so I can consider to reopen.

Please understand that we can't take your word for it and won't try to reproduce by a mere description.

This seems to be a regression introduced in #1503

@LinusBorg ghost is right, there's no way to get exact production build, but with staging/test env.

See my last comment.

@LinusBorg this is still an issue, and the reproduction has been there since 9 Jun 2018

the codesandbox that is provided by OP does not use Vue CLI 3 (its an old Vue CLI 2 template), so is obviously not a valid reproduction.

Other reproductions were not provided.

Ok never mind.

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

Edit: NODE_ENV works when the .env.staging file has no trailing # comments - see https://github.com/motdotla/dotenv#rules

Glad you could solve your problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BusyHe picture BusyHe  路  3Comments

JIANGYUJING1995 picture JIANGYUJING1995  路  3Comments

CodeApePro picture CodeApePro  路  3Comments

wahidrahim picture wahidrahim  路  3Comments

Akryum picture Akryum  路  3Comments