React process.env.NODE_ENV #2035
Answered
by
xc2
maxim-semikov
asked this question in
Q&A
-
Hello! if (process.env.NODE_ENV === 'development') {
console.log('this is a development log');
} and I got an error: Uncaught ReferenceError: process is not defined |
Beta Was this translation helpful? Give feedback.
Answered by
xc2
Apr 8, 2024
Replies: 2 comments 3 replies
-
Hi, I can not reproduce this issue, can you provide a reproduction repo? |
Beta Was this translation helpful? Give feedback.
0 replies
-
this is repo https://github.com/maxim-semikov/rsbuild-env |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please use
process.env.NODE_ENV === 'development'
instead ofprocess?.env?.NODE_ENV === 'development'
.This is a common misconception about the "webpack definePlugin".
nowadays the term "define" in js building field typically means replacing expression instead of declare a variable.
e.g.
define: {"process.env.NODE_ENV": '"production"'}
will transformprocess.env.NODE_ENV === 'development'
into"production" === 'development'
but notconst process = {env: {NODE_ENV: "production"}}; process.env.NODE_ENV === 'development'
;process?.env?.NODE_ENV
doesn't match the expressionprocess.env.NODE_ENV
so it is not replaced.