Missing .env in Vue CLI after run build - vue.js

I'm building an app using Vue CLI 3.
I've included .env in my project and everything works fine.
But when i'm building the app for production via npm run build there is no .env file in my dist folder, so i can't change my environmental variables in production server.
Any solution or it's just fine?

This is supposed to happen. The env variables are embedded in your build.
You can make seperate .env files for production. These variables will be used during the production build.
Create a new .env file named: .env.production
Source: https://cli.vuejs.org/guide/mode-and-env.html#modes

It is normal because application needed to be recompiled when .env file changed.
For convenience you can use .env.prod or .env.dev for your CI/CD pipeline.

Related

Problem with Vue3 Vite .env during development mode

I have problem with acces the .env variables.
In my Vite cli project I'v added .env variables:
VITE_SOME_KEY = 123
and then in js file:
console.log(import.meta.env.DEV);
console.log(import.meta.env.VITE_SOME_KEY);
It's working after build and serve. But during development (npm run vite) i have problem:
Module "process" has been externalized for browser compatibility. Cannot access "process.env" in client code.
I check all documentation from vite enviroments but I don't understand how to use .env during development localy

Getting environment variables at runtime in a compiled VueJS app

I'm working on a VueJS app that uses env vars in multiple places. It runs on GCloud with nginx serving the files compiled with vue-cli-service build.
When developing everything works well with the env vars set in .env.development and .env.development.local files and used in JS with process.env.VUE_APP_FOO. I'm not using .env.production as some of these env vars shouldn't be committed to our repository.
For the staging and prod environment of all of our projects, we use GCloud's config maps which let us provide env vars to the pods. The issue in this project is that vue-cli-service build requires the env vars to be available at build time, which is not the case in our setup. Config maps are only available in the pods that run the images.
Out of curiosity, I checked the compiled code and all uses of process.env are quite simply replaced by an object with all vue env vars (basic ones + VUE_APP_* ones). So for example,
console.log(process.env.VUE_APP_FOO);
is compiled to
console.log(Object({NODE_ENV: "production", BASE_URL: "/", VUE_APP_FOO: "bar"}).VUE_APP_FOO);
Except that in our case, VUE_APP_FOO is missing from the object as it's not available in the environment when building the app.
So as is, it doesn't seem possible to provide env vars when the server is started or the JS file is served. Is there a way to tell vue-cli-service to not compile the env that way? Or any other alternative?
The only one I found so far is to replace the uses of env vars with their actual value directly in the compiled JS file when the pod starts using sed, but that's pretty ugly and could break easily.
One approach you can follow is to provide the production values when building locally. Another approach is to setup a continuous integration workflow that fetches your environment variables from wherever it is stored, builds the apps and pushes to production servers. I personally work with approach 2.
It is relatively easy to setup a github workflow that runs whenever your code is pushed to a particlar branch

How to access react-native-config ENV variables in babel.config.js?

I'm using react-native-config for setting environment variables in my react-native app. Now I want to use the ENV environment variable in the babel.config.js file to apply a plugin only when running the app in production. Basically what I want to do is apply a babel plugin only when running in production. Is this the correct way to achieve my goal?
How can I use the variable in the babel.config.js file? I tried importing Config from the react-native-config package and tried to access the variable via Config.ENV but that doesn't seem to work.

In Vue cli, How to use custom mode and build like in production mode?

I'm building a Vue app and want to override some env variables while building in one build case, so I created a custom mode and sat a command in package.json like:
vue-cli-service build --mode myMode --modern
And put the env vars I want in the file specified for this mode:
// file name: .env.myMode
VUE_APP_MY_VAR=123
Now how can I build using the same build steps in production mode but within my custom mode?
Because when I build using the previous command it doesn't mangle or compress the js files for example.
Just add NODE_ENV=production into .env.myMode
Docs
Then NODE_ENV will determine the primary mode your app is running in - development, production or test - and consequently, what kind of webpack config will be created.

Vue webpack environment variables configuration

I am new to vue in general and i am trying to configure some environment variables for some projects of mine so i can do some tests with cookies but apperently i ran the simple webpack configurations when creating these projects, therefore i dont have access to the config directory to edit said variables.
I created a vue.config.js file and used the following lines:
module.exports = {
publicPath: 'myAppName'
}
However if i run it on development mode, or simply use npm run serve my app runs at "http://localhost:8080/myAppName" instead of simply "myAppName".
How do i correctly configure my environment variables for my projects without having to start over from scratch? I am using vueCli 3 btw.
I tried following these examples but none have worked:
Using Environment Variables with Vue.js
I also have .env file and a .env.development file but i am not sure what to add to it.