Library Mode replaces "process.env.NODE_ENV"
See original GitHub issueDescribe 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:
- Created 2 years ago
- Reactions:7
- Comments:11 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to preserve
process.env.NODE_ENV
in the build files by adding the following to the config:EDIT: Actually this throws in serve mode, so you have to conditionally define it in production mode only:
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:
https://github.com/ipfs/js-ipfs-utils/blob/b319e67a47268870f9defc4c7bfa7c1bb450f424/src/env.js#L13