Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dotenv with Vue.js

Tags:

I'm trying to add some environment variables into my vue app.

here is content of my .env file, which is placed on root(outside src):

VUE_APP_GOODREADS_KEY = my_key 

and I added code for dot env on the top of my main.js

import Vue from 'vue' import App from './App' import VueResource from 'vue-resource' import Vuetify from 'vuetify'  import dotenv from 'dotenv'  dotenv.config()  import { router } from './router' import store from './store' 

I want to use this variable within my store ./store/index.js

I tried to console.log environment variables within store but no luck:

console.log(process.env) 

But all I'm getting is

NODE_ENV:"development" 

Only related thread I found was Load environment variables into vue.js, but it only mentions how to use existing variables in the process.env. I want to add environment variables using dotenv. Why this code is not working?

like image 802
Dani Vijay Avatar asked Jun 06 '18 08:06

Dani Vijay


People also ask

Where do I put .env files in VUE?

This is a quick tutorial on how to create and access environment variables in Vue 3 with a dotenv ( . env ) file. Vue 3 apps built with Vite support dotenv environment variables out of the box, so all you need to do is add a . env file to the root folder of your Vue project.

What is .env file in VUE JS?

env file that stores all of our environment variables. This file is typically private, and can be used to store things like API keys, URLs, and other things that are specific to one environment. Vue. js also lets us use .

Can I use dotenv in frontend?

Dotenv is wonderful. It allows you to use environment variables in your code, hence separating the code from its running environment. The problem I have with it is that it is common to see people using it in the front-end of their web application (served static files, non-SSR applications).

How do I use dotenv packages?

To use DotEnv, first install it using the command: npm i dotenv . Then in your app, require and configure the package like this: require('dotenv'). config() .


2 Answers

If your project was create by using Vue CLI 3, no need to use dotenv to get environment variables.

To get environment variables within .env file in your project:

  1. Placing the .env file in your project root.
  2. In the .env file, specifying environment variables with "VUE_APP_" prefix.

    VUE_APP_SOMEKEY=SOME_KEY_VALUE.

  3. Finally, you can access them with process.env.* in your application code.

    console.log(process.env.VUE_APP_SOMEKEY) // SOME_KEY_VALUE

Referance: Vue CLI 3 - Environment Variables and Modes

like image 116
white Avatar answered Sep 28 '22 12:09

white


When using Vue CLI 2, you have to use the dev.env.js and prod.env.js files located in the config folder.

Vue CLI 2 does not support the use of .env files, however Vue CLI 3 does.

// /config/prod.env.js 'use strict' module.exports = {   NODE_ENV: '"production"',   SERVER_URI: "http://someremoteuri:3333/api/v1" } 
// /config/dev.env.js 'use strict' module.exports = {   NODE_ENV: '"development"',   SERVER_URI: "http://localhost:3333/api/v1" } 
like image 41
PrestonDocks Avatar answered Sep 28 '22 10:09

PrestonDocks