Problem with Vue3 Vite .env during development mode - process

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

Related

How can I use environment variables if I use vuejs with CDN?

I was using Vuejs CDN to develop my app. Now, I want to separate dev and prod since the API endpoints are separated. I don't want to expose my dev API endpoint. How can I do it with the simplest way?
I tried to use dotenv. Put the DEBUG variable in .env.
var debug = process.env.DEBUG;
console.log(debug);
Vue.directive('focus', {
inserted: function (el) {
el.focus()
}
})
var vm = new Vue({...
My .env file
DEBUG=true
I can read the debug variable. However, it gives an error of "Vue is not defined" since I didn't install Vue with npm. Or I must install Vue with npm?
If you're using Vue CLI, facilities for this are already built in in the form of .env files. You'd create .env.dev and .env.production files (and probably a .env.local for local development), containing:
VUE_APP_ENDPOINT=https://...
Which in your Javascript files you can access as:
const endpoint = process.env.VUE_APP_ENDPOINT;
Then you create different builds for your two different environments:
vue-cli-service build --mode dev # or --mode production
The variables will be baked into the build, so your two different builds only contain their respective endpoints.

vue create command: is webpack and loader configured automatically

Is webpack configured automatically in the vue create hello-world generated project? Is any loader included by default? Like css-loader?
Yes, it is. you can run vue-cli-service inspect to check the webpack config. For more information Inspecting the Project's Webpack Config

Missing .env in Vue CLI after run build

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.

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.