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

NODE_ENV is undefined #16960

Closed
PointSingularity opened this issue May 11, 2023 · 8 comments · Fixed by #17251 or #17375
Closed

NODE_ENV is undefined #16960

PointSingularity opened this issue May 11, 2023 · 8 comments · Fixed by #17251 or #17375
Assignees
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@PointSingularity
Copy link

Current Behavior

Creating a new standalone Node application with NX 16.1.0 and console logging process.env.NODE_ENV gives back undefined,
even though it was always defined in versions 15 anb below.

Using --prod during build also doesn't change anything.

Expected Behavior

process.env.NODE_ENV='development' during development.
process.env.NODE_ENV='production' after using build to do a production build.

GitHub Repo

No response

Steps to Reproduce

  1. Create a standalone Node application with 16.1.0
  2. Put console.log(process.env.NODE_ENV) in main.ts and run it
  3. Additionally build the application and run the main.js from the dist folder.

Nx Report

Node   : 18.15.0
   OS     : win32 x64
   npm    : 9.6.3
   Hasher : Native

   nx                 : 16.1.0
   @nx/js             : 16.1.0
   @nx/jest           : 16.1.0
   @nx/linter         : 16.1.0
   @nx/workspace      : 16.1.0
   @nx/devkit         : 16.1.0
   @nx/esbuild        : 16.1.0
   @nx/eslint-plugin  : 16.1.0
   @nx/node           : 16.1.0
   @nrwl/tao          : 16.1.0
   typescript         : 5.0.4


### Failure Logs

_No response_

### Operating System

- [ ] macOS
- [ ] Linux
- [X] Windows
- [ ] Other (Please specify)

### Additional Information

_No response_
@AgentEnder AgentEnder added the scope: node Issues related to Node, Express, NestJS support for Nx label May 12, 2023
@renambot
Copy link

renambot commented May 17, 2023

same problem. (macOS or Linux docker)

@oben01
Copy link

oben01 commented May 18, 2023

same problem also

@cuddlecake
Copy link

Other executors set NODE_ENV, e.g.

(process.env as any).NODE_ENV ||= isScriptOptimizeOn
? 'production'
: 'development';

but the node executor does not, as far as I can see:
https://github.com/nrwl/nx/blob/master/packages/js/src/executors/node/node.impl.ts

@janeklb
Copy link
Contributor

janeklb commented May 23, 2023

temp workaround

// in your webpack.config.ts

// workaround for undefined NODE_ENV
const withFixedNodeEnv: () => NxWebpackPlugin =
  () =>
  (config, { context }) => {
    if (config.mode && config.mode !== 'none') {
      return config;
    }
    if (context.configurationName === 'production') {
      config.mode = 'production';
    } else {
      config.mode = 'development';
    }
    return config;
  };

export default composePlugins(withNx(), withFixedNodeEnv(), (config) => {
  // Update the webpack config as needed here.
  // e.g. config.plugins.push(new MyPlugin())
  // For more information on webpack config and Nx see:
  // https://nx.dev/packages/webpack/documents/webpack-config-setup
  return config;
});

(which requires that you have a configuration named production for your build task... or just repurpose the above with whatever works for you)

@ndcunningham ndcunningham self-assigned this May 24, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue May 26, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue May 26, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue May 29, 2023
@PointSingularity
Copy link
Author

@ndcunningham Thank you for fixing this for esbuild! Sadly, this is also present for webpack too, so I don't think it's fully resolved yet.

@bojanbass
Copy link

I'm not sure the fix that was merged has anything to do with @nx/js:node serve and @nx/webpack:webpack executors, as one of those is causing this error. I think this bug report has to bug reopened.

@bojanbass
Copy link

A temporary workaround from @janeklb does not work in my case. I have managed to run it properly using .env configs per target and configuration:

  • .env.serve.development (NODE_ENV=development)
  • .env.serve.production (NODE_ENV=production)

This way, serving the app with the --prod flag or without loads the correct .env file. But still, this is quite a hack and a proper solution should be implemented.

I did some debugging and my closest bet is that this PR broke the previously working functionality: dc71229

I suppose executing a buildTarget with npx nx run command looses process.env.NODE_ENV value. Another strange thing is that when triggering the serve target and inspecting the build target which uses @nx/webpack:webpack under the hood it does not output any console.log statement. Still, if I trigger the build target directly, it does. In previous NX versions, this was also working correctly.

@jaysoo any idea on that one?

ndcunningham added a commit to ndcunningham/nx that referenced this issue Jun 1, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue Jun 1, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue Jun 1, 2023
ndcunningham added a commit to ndcunningham/nx that referenced this issue Jun 1, 2023
@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 Jun 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
8 participants