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.

Library Mode replaces "process.env.NODE_ENV"

See original GitHub issue

Describe the bug

process.env.NODE_ENV gets replaced and removes code in library mode. This is an undesirable behaviour as i want to have this check in the project i’m going to import the library.

Reproduction

export const testFunction = () => {
  if (process.env.NODE_ENV === 'development') {
    alert('This is a Test');
  }
}

if build with vite build using this config:

import { defineConfig } from 'vite';
import * as path from 'path';

export default defineConfig({
  build: {
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      name: 'myProject',
    },
  },
});

it will become:

const testFunction = () => {
};

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: Windows 10 10.0.19041
    CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
    Memory: 8.36 GB / 15.94 GB
  Binaries:
    Node: 12.16.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.5 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.5 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 90.0.4430.93
    Edge: Spartan (44.19041.906.0), Chromium (90.0.818.51)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    vite: ^2.2.3 => 2.2.3

Used package manager: npm


Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
LeBenLeBencommented, Feb 2, 2022

I was able to preserve process.env.NODE_ENV in the build files by adding the following to the config:

define: {
  'process.env.NODE_ENV': 'process.env.NODE_ENV',
},

EDIT: Actually this throws in serve mode, so you have to conditionally define it in production mode only:

import { defineConfig } from 'vite';

export default defineConfig(({ mode }) => {
  const isProd = mode === 'production';

  const config = {
    // ...
  };
  
  if (isProd) {
    config.define = {
      'process.env.NODE_ENV': 'process.env.NODE_ENV',
    };
  }

  return config;
});
1reaction
melMasscommented, Apr 10, 2022

Hello,

I"ve read all the related issue but I don’t understand how to solve it for external libraries, in my case js-ipfs:

image https://github.com/ipfs/js-ipfs-utils/blob/b319e67a47268870f9defc4c7bfa7c1bb450f424/src/env.js#L13

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is process.env.NODE_ENV undefined? - Stack Overflow
process.env is a reference to your environment, so you have to set the variable there. To set an environment variable in Windows: SET...
Read more >
Customizing Node.js .env files - LogRocket Blog
Learn how to configure a Node.js application via environment variables and customize an .env file with new environment variables.
Read more >
Env Variables and Modes - Vite
During production, these env variables are statically replaced. It is therefore necessary to always reference them using the full static string.
Read more >
Webpack 4 Automatically Makes process.env.NODE_ENV ...
Ben Nadel just discovered that Webpack 4 will implicitly use the DefinePlugin to automatically replace the substring, "process.env.
Read more >
Building Better Bundles: Why process.env.NODE_ENV ...
Node.js exposes the current process's environment variables to the script ... should be running in “development” mode vs “production” mode.
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