question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to use modes and environment variables in vite.config.js ?

See original GitHub issue

I try to use env variables in vite.config.js like this:

const isDev1 = import.meta.env.MODE === 'development'
const isDev2 = process.env.MODE === 'development'
const dev_port1 = import.meta.env.VUE_DEVPORT
const dev_port2 = process.env.VUE_DEVPORT
module.exports = {
    base: './',
    port: dev_port1,
    sourcemap: isDev1,
    optimizeDeps: {
        include: ['axios']
    }
}

Environment variables cannot be accessed either way,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Aferzcommented, Jul 10, 2020

@hyingreborn you are right about NODE_ENV. It’s not set yet in vite.config.js. I don’t know if it intentional by design but you could create your own scripts in your package.json:

"scripts": {
    "build:dev": "APP_ENV=development vite build",
    "build:prod": "APP_ENV=production vite build",
}

In your vite.config.js, now you can manipulate it:

const mode = process.env.APP_ENV // This now exists.

module.exports = {
  mode: mode, // This will set the mode, to avoid confusions.
}
0reactions
yyx990803commented, Feb 4, 2021

https://vitejs.dev/config/#conditional-config

P.S this issue is way out of date.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Env Variables and Modes - Vite
Vite exposes env variables on the special import.meta.env object. ... An env file for a specific mode (e.g. .env.production ) will take higher...
Read more >
How can I use Vite env variables in vite.config.js?
You can load the app level env variables and add them to the Node level env variables: import { defineConfig, loadEnv } from...
Read more >
How to use a .env file with Vite and Vue? - Samanja Cartagena
env file can be used by using env.variable in the config file except in the config file load env needs to be imported...
Read more >
Environment Variables and Modes in Vite - Vue School
In this lesson, we learn about the value of environment variables, how to define them with .env files, and how to access them...
Read more >
Handling process.env - Quasar Framework
`.env` file // If you want .env file to override the terminal variables, // use `require('dotenv').config({ override: true })` instead return ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found