@@ -393,6 +393,11 @@ export async function bundleWorker(
393393
394394 const unenvResolvePaths = getUnenvResolvePathsFromEnv ( ) ?. split ( "," ) ;
395395
396+ // Use `process.env["NODE_ENV" + ""]` to get the runtime value when wranler is executed.
397+ // Esbuild would replace `process.env.NODE_ENV` with the value it has when wrangler gets bundled.
398+ // See: https://github.com/cloudflare/workers-sdk/issues/1477)
399+ const runtimeProcessEnv = process . env [ "NODE_ENV" + "" ] ;
400+
396401 const buildOptions = {
397402 // Don't use entryFile here as the file may have been changed when applying the middleware
398403 entryPoints : [ entry . file ] ,
@@ -422,18 +427,16 @@ export async function bundleWorker(
422427 metafile : true ,
423428 conditions : getBuildConditions ( ) ,
424429 platform : getBuildPlatform ( ) ,
425- ...( process . env . NODE_ENV && {
426- define : {
427- ...( defineNavigatorUserAgent
428- ? { "navigator.userAgent" : `"Cloudflare-Workers"` }
429- : { } ) ,
430- // use process.env["NODE_ENV" + ""] so that esbuild doesn't replace it
431- // when we do a build of wrangler. (re: https://github.com/cloudflare/workers-sdk/issues/1477)
432- "process.env.NODE_ENV" : `"${ process . env [ "NODE_ENV" + "" ] } "` ,
433- ...( nodejsCompatMode === "legacy" ? { global : "globalThis" } : { } ) ,
434- ...define ,
435- } ,
436- } ) ,
430+ define : {
431+ ...( defineNavigatorUserAgent
432+ ? { "navigator.userAgent" : `"Cloudflare-Workers"` }
433+ : { } ) ,
434+ ...( runtimeProcessEnv
435+ ? { "process.env.NODE_ENV" : `"${ runtimeProcessEnv } "` }
436+ : { } ) ,
437+ ...( nodejsCompatMode === "legacy" ? { global : "globalThis" } : { } ) ,
438+ ...define ,
439+ } ,
437440 loader : COMMON_ESBUILD_OPTIONS . loader ,
438441 plugins : [
439442 aliasPlugin ,
0 commit comments