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.

import.meta.env.MODE=production while import.meta.env.PROD returned false

See original GitHub issue

Describe the bug

When I set NODE_ENV=development in .env file, and set NODE_ENV=production in .env.production file, then run vite build with production mode(by default), the import.meta.env.PROD variable turned out to be false, which is expected to be true.

Screen Shot 2021-12-29 at 11 04 38 PM

I found that at vite/packages/vite/src/node/config.ts, there is a loadEnv function, in which process.env.VITE_USER_NODE_ENV would be override by .env file

https://github.com/vitejs/vite/blob/d856c4bd6798707e0cbdfc127a2e8b6c00c65dae/packages/vite/src/node/config.ts#L1012-L1023

https://github.com/vitejs/vite/blob/d856c4bd6798707e0cbdfc127a2e8b6c00c65dae/packages/vite/src/node/config.ts#L353

when vite build, isProduction would be false.

In this case, I think maybe change the condition below may work:

// change this
if (key === 'NODE_ENV') { xxx }
// to
if (key === 'NODE_ENV' && process.env.VITE_USER_NODE_ENV === undefined) { xxx }

Reproduction

https://github.com/RainKolwa/reproduce-vite-env

System Info

System:
    OS: macOS 12.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 115.40 MB / 8.00 GB
    Shell: 3.1.2 - /usr/local/bin/fish
  Binaries:
    Node: 14.17.5 - ~/.nvm/versions/node/v14.17.5/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.14 - ~/.nvm/versions/node/v14.17.5/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Firefox: 92.0
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-vue: ^2.0.0 => 2.0.1
    vite: ^2.7.2 => 2.7.9

Used Package Manager

pnpm

Logs

vite:config bundled config file loaded in 64.31ms +0ms
[dotenv][DEBUG] did not match key and value when parsing line 2:
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: { open: true, fs: { strict: true, allow: [Array], deny: [Array] } },
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: '/Users/rainkolwa/Personal/reproduce-vite-env/dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {
  vite:config       input: '/Users/rainkolwa/Personal/reproduce-vite-env/index.html'
  vite:config     },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/rainkolwa/Personal/reproduce-vite-env/vite.config.js',
  vite:config   configFileDependencies: [ 'vite.config.js' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/rainkolwa/Personal/reproduce-vite-env',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/rainkolwa/Personal/reproduce-vite-env/public',
  vite:config   cacheDir: '/Users/rainkolwa/Personal/reproduce-vite-env/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: false,
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: true,
  vite:config     proxy: undefined,
  vite:config     cors: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   }
  vite:config } +186ms
vite v2.7.9 building for production...
✓ 9 modules transformed.
dist/index.html                  0.38 KiB
dist/assets/index.b5862e96.js    1.17 KiB / gzip: 0.66 KiB
dist/assets/vendor.af2b58ca.js   49.45 KiB / gzip: 19.96 KiB

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
RainKolwacommented, Dec 31, 2021

I think we also need this PR to make sure NODE_ENV is handled correctly.

1reaction
bluwycommented, Dec 30, 2021

I think .env should have a lower priority, and should be fixed. It makes sense for scoped .env to overrides the unscoped env files.

EDIT: From the code, looks like it was intentional. I think we should document instead 🤔

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. Some built-in variables are available in all cases: import.meta.env.
Read more >
Import.meta.env undefined on production build vitejs
I am using vitejs to compile my react app statically, however after build . env imports become undefined which is not the case...
Read more >
rollup-plugin-import-meta-env - npm
The plugin exposes env variables on the special import.meta.env object. Config. env({ PROD: true, ...
Read more >
Should the browser consider an import.meta.env object? - WICG
The most typical use case for this is the process.env.NODE_ENV === 'production' check which has become a JS idiom and not just a...
Read more >
Optimizing Performance - React
Browserify. For the most efficient Browserify production build, install a few plugins: # If you use npm npm install --save-dev envify ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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