Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.env.NODE_ENV is undefined #14547

Closed
asherccohen opened this issue Jan 23, 2023 · 16 comments
Closed

process.env.NODE_ENV is undefined #14547

asherccohen opened this issue Jan 23, 2023 · 16 comments
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@asherccohen
Copy link

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

@AgentEnder AgentEnder added blocked: more info needed scope: react Issues related to React support for Nx labels Jan 23, 2023
@AgentEnder
Copy link
Member

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.

@asherccohen
Copy link
Author

asherccohen commented Jan 23, 2023

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?

@asherccohen
Copy link
Author

I apologize for being demanding, is there any news on this issue?

All my projects are blocked and I haven't found a valid workaround.

Is there another way to read when apps are running in development or production?

@nemonemi
Copy link

nemonemi commented Jan 26, 2023

One way to proceed could be to downgrade NX to something below 15.5.0. I'm on 15.4.2 and process.env.NODE_ENV shows the correct value.
This is in no way a solution, but it might unblock you.

@asherccohen
Copy link
Author

Thanks but I can't do that at the moment.

I've also noticed that NX_BASE is ignored when running CLI commands, could it be related?

@farhin00farhin
Copy link

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

@asherccohen
Copy link
Author

asherccohen commented Jan 27, 2023

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.

@s-bauer
Copy link

s-bauer commented Feb 1, 2023

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

@cgutierrezpa
Copy link

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

@cgutierrezpa
Copy link

cgutierrezpa commented Feb 6, 2023

Any ideas on how can I set NODE_ENV to work around this issue? Every time I set NODE_ENV via .env file or via command it is unset again. I provided more context in this other related thread

@farhin00farhin
Copy link

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 🙏🏼

@octadecimal
Copy link

Just chiming in to mention we're experiencing the same issue described above. NODE_ENV is no longer being provided, even with a freshly generated React app. This has caused a cascading set of issues for us, and is preventing us from migrating/upgrading to the latest Nx.

@qweluke
Copy link

qweluke commented Feb 10, 2023

I am experiencing the same issue! 😡

for now, i am running the app like this:

export NODE_ENV=development && nx serve APP_NAME --port=3001

hope them solve this issue quickly!

@PointSingularity
Copy link

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

@ndcunningham
Copy link
Contributor

This should be fixed with the latest version.
Please reopen if the problem still persists

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

No branches or pull requests

10 participants