nx: process.env.NODE_ENV is undefined

Current Behavior

nx run my-ui:serve:development

logging process.env.NODE_ENV returns undefined

Expected Behavior

nx run my-ui:serve:development

logging process.env.NODE_ENV returns “development”

Github Repo

No response

Steps to Reproduce

nx run my-ui:serve:development

Nx Report

Node : 18.13.0
   OS   : linux x64
   npm  : 8.19.3
   
   nx : 15.5.2
   @nrwl/angular : Not Found
   @nrwl/cypress : 15.5.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.5.2
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.5.2
   @nrwl/expo : Not Found
   @nrwl/express : 15.5.2
   @nrwl/jest : 15.5.2
   @nrwl/js : 15.5.2
   @nrwl/linter : 15.5.2
   @nrwl/nest : Not Found
   @nrwl/next : 15.5.2
   @nrwl/node : 15.5.2
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 15.5.2
   @nrwl/react-native : Not Found
   @nrwl/rollup : 15.5.2
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.5.2
   @nrwl/web : 15.5.2
   @nrwl/webpack : 15.5.2
   @nrwl/workspace : 15.5.2
   @nrwl/vite : Not Found
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
         @nrwl/remix: 14.4.2

Failure Logs

No response

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 5
  • Comments: 16 (2 by maintainers)

Most upvoted comments

Same issue for me, happend with my latest upgrade from 15.4.5 to 15.6.3

I believe it was introduced in 15.4.6. Works fine in 15.4.5

Just checked, and it was indeed introduced in 15.4.6

Hey @asherccohen! Can you provide either more thorough repro steps or a reproduction which we can clone? As is, we don’t really know how you generated the app, what kind of bundler or anything like that which is in use so it would be hard to diagnose any issues effectively.

Running into the same issue. It’s pretty easy to recreate. Create any new repo with below options image and then try to get process.env.NODE_ENV

I believe it was introduced in 15.4.6. Works fine in 15.4.5

@AgentEnder any update on this? Fairly easy to reproduce and check through the changes introduced in 15.4.6. Happy to help with more info if needed and would appreciate keeping us posted with any updates 🙏🏼

Hey @asherccohen! Can you provide either more thorough repro steps or a reproduction which we can clone? As is, we don’t really know how you generated the app, what kind of bundler or anything like that which is in use so it would be hard to diagnose any issues effectively.

Running into the same issue. It’s pretty easy to recreate. Create any new repo with below options image and then try to get process.env.NODE_ENV

I believe it was introduced in 15.4.6. Works fine in 15.4.5

@jaysoo already has a fix for the development mode #14915

It’s still an issue on 15.6.

I’ve been debugging this a bit and searched for all the places where process.env.NODE_ENV is used. At every step it’s “undefined”, here’s the list:

nx/src/executors/run-commands/run-commands.impl.ts webpack/src/executors/webpack/webpack.impl.ts

nx/src/tasks-runner/forked-process-task-runner.ts // particularly here “dotenv.parse(envContents)” is not able to read NODE_ENV

webpack/src/utils/with-web.ts webpack/src/utils/get-client-environment.ts webpack/src/utils/webpack/interpolate-env-variables-to-index.ts webpack/src/utils/with-nx.ts

I can force NODE_ENV manually in .env or create a run-command to conditionally serve:development/serve:production

    "serve:development": {
      "root": "apps/my-app",
      "executor": "nx:run-commands",
      "options": {
        "commands": [
          {
            "command": "export NODE_ENV=development; npm run nx run my-app:serve"          }
        ],
        "parallel": false
      }
    },
    "serve:production": {
      "root": "apps/my-app",
      "executor": "nx:run-commands",
      "options": {
        "commands": [
          {
            "command": "export NODE_ENV=production; npm run nx run my-app:serve"
          }
        ],
        "parallel": false
      }
    }

Fun fact is that the “build” command instead is able to read process.env.NODE_ENV without an issue. Therefore the problem might be with the serve task-runner.

So I’m unblocked, hopefully this is useful to get a proper fix.

hi @AgentEnder , sorry for the lack of info and thanks for the quick answer.

Hard to give proper steps, I had the monorepo since a long time and noticed the issue while upgrading to 15.5.2.

Currently it’s happening on a react app, but I’ll check if it happens in libs too.

It was created with

nx g @nrwl/react:app my-new-app

And here’s the project.json:

    "serve": {
      "executor": "@nrwl/webpack:dev-server",
      "options": {
        "buildTarget": "app-name:build",
        "hmr": true,
        "port": 4203
      },
      "configurations": {
        "production": {
          "buildTarget": "app-name:build:production",
          "hmr": false
        },
        "development": {
          "buildTarget": "app-name:build:development"
        }
      },
      "defaultConfiguration": "development"
    },
    
    ```
    
    for building I use the default:
        ```
        "build": {
      "executor": "@nrwl/webpack:webpack",
      ...other configs
   

Is there something else I could provide?