'npm run serve vs build

In my Vue JS application I have a file called .env.individual which defines a variable use for making API calls to the backend.

I also have .env and .env.production, etc, all with different values for the API URL variable.

When I run npm run serve -- --mode individual the application starts up and uses the URL found in the .env.individual file. Likewise, when I run npm run serve -- --mode production the application starts up and uses the variable found in the .env.production file.

Given the above I was assuming that when I run npm run build -- --mode individual the \dist would be generated and I could then run npm run serve and the application would use the variables found in the .env.individual file.

Given my package.json file contains this:

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "deploy": "vue-cli-service s3-deploy",
    "release": "npm run build && npm run deploy"
},

What is npm run serve actually doing and why - when I want to use a specific .env.XXX file do I need to specify it exactly?



Solution 1:[1]

npm run serve does not run your application from /dist folder. It compiles unoptimized build in memory (RAM). If you want run your optimized build from /dist folder, you can run it by some http server. For example https://www.npmjs.com/package/http-server .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matus Mucka