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 #251

Closed
2 tasks done
karlvr opened this issue Nov 11, 2021 · 11 comments · Fixed by #252 or #316
Closed
2 tasks done

NODE_ENV is undefined #251

karlvr opened this issue Nov 11, 2021 · 11 comments · Fixed by #252 or #316
Assignees

Comments

@karlvr
Copy link

karlvr commented Nov 11, 2021

  • Tried the troubleshooting Wiki
  • Followed the migration Wiki

Describe the bug
After upgrading from 3.2.0 to 3.3.0 my use of process.env.NODE_ENV returns undefined. It seems that 3.3.0 added support for process.env, but maybe clobbers NODE_ENV? I tried added to the blocklist, and I have safe set to true, but nothing seemed to change the behaviour except downgrading to 3.2.0.

To Reproduce
Steps to reproduce the behavior:

  1. Upgrade to 3.3.0
  2. console.log(process.env.NODE_ENV)
  3. Observe that it outputs undefined instead of development or production

Expected behavior
The process.env.NODE_ENV should be untouched.

@github-actions
Copy link

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/251

@goatandsheep
Copy link
Owner

Hi @karlvr I created a branch that added NODE_ENV to the process.env test, which seemed to pass (check PR #252 ). I didn't see your thread in the discussions section, but that's okay we're here now. I'm happy to assist further following priority boost

@tgwow
Copy link

tgwow commented Dec 13, 2021

Hello guys, I'm also having this behavior using multi-env.

I can only use multi-env if I set APP_ENV. My question is if i can have problems with NODE_ENV == undefined?

babel.config.js

api.cache.invalidate(() => process.env.NODE_ENV);
  console.log('env', process.env.NODE_ENV);
  console.log('benv', process.env.BABEL_ENV);
  console.log('appenv', process.env.APP_ENV);
  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: [
      [
        'module:react-native-dotenv',
        {
          moduleName: '@env',
          path: '.env',
          safe: false,
          allowUndefined: true,
        },
      ]...

package.json

"start:dev": "APP_ENV=development expo start -c",
"start:prod": "APP_ENV=production expo start -c"

running production script the logs are:

transform[stdout]: env undefined
transform[stdout]: benv development
transform[stdout]: appenv production

the BABEL_ENV=development was expected?

@goatandsheep
Copy link
Owner

I figured out how to reproduce the error and I'm investigating

@tgwow
Copy link

tgwow commented Dec 13, 2021

@goatandsheep thank you for the quick answer. I'm building with expo ba --releaseChannel staging, but it always points to .env.production, do you think it has something to do with the above problem? The "benv" log outputs production.

@goatandsheep
Copy link
Owner

I figured out how to fix @karlvr 's problem.

@tgwow APP_ENV is not recommended behaviour and using development and production is not supposed to work using APP_ENV. Recommended behaviour is to use NODE_ENV=development. See the docs https://github.com/goatandsheep/react-native-dotenv#override-envname

@goatandsheep
Copy link
Owner

@tgwow look into creating a dev build https://docs.expo.dev/development/getting-started/

@allandiego
Copy link

allandiego commented Mar 13, 2022

im having a similar strange behavior using expo 44 and "react-native-dotenv": "^3.3.1"
process.env.NODE_ENV return undefined but process.env have value i dont know how its possible

console.log(process.env);

/*
output:
Object {
  "NODE_ENV": "development",
}
*/

console.log(process.env.NODE_ENV);
/*
output:
undefined
*/

schiller-manuel added a commit to schiller-manuel/react-native-dotenv that referenced this issue May 2, 2022
This fixes goatandsheep#251 (comment)

react-native sets the `NODE_ENV` **after** this transformation happens.
Code that directly access `process.env.NODE_ENV` was replaced by `babelMode` during transformation (if latest babel was used) or by `undefined`.

With this commit, any access to a `process.env` variable is only replaced by the corresponding value if its value is set.
schiller-manuel added a commit to schiller-manuel/react-native-dotenv that referenced this issue Jun 7, 2022
This fixes goatandsheep#251 (comment)

react-native sets the `NODE_ENV` **after** this transformation happens.
Code that directly access `process.env.NODE_ENV` was replaced by `babelMode` during transformation (if latest babel was used) or by `undefined`.

With this commit, any access to a `process.env` variable is only replaced by the corresponding value if its value is set.
schiller-manuel added a commit to schiller-manuel/react-native-dotenv that referenced this issue Jun 7, 2022
This fixes goatandsheep#251 (comment)

react-native sets the `NODE_ENV` **after** this transformation happens.
Code that directly access `process.env.NODE_ENV` was replaced by `babelMode` during transformation (if latest babel was used) or by `undefined`.

With this commit, any access to a `process.env` variable is only replaced by the corresponding value if its value is set.
@douglascavanaugh
Copy link

douglascavanaugh commented Jul 7, 2022

RN 0.68.1/react-native-dotenv 3.3.1: If the output of "process.env.NODE_ENV" is undefined, don't set NODE_ENV on "process.env.NODE_ENV", set it on the const value:

const environment = process.env
console.log(environment.NODE_ENV)

@exoriri
Copy link

exoriri commented Aug 19, 2022

@douglascavanaugh thanks, really helped. But why this trick with const environment = process.env works?

@goatandsheep goatandsheep reopened this Sep 14, 2022
@github-actions
Copy link

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/251

goatandsheep pushed a commit that referenced this issue Oct 12, 2022
This fixes #251 (comment)

react-native sets the `NODE_ENV` **after** this transformation happens.
Code that directly access `process.env.NODE_ENV` was replaced by `babelMode` during transformation (if latest babel was used) or by `undefined`.

With this commit, any access to a `process.env` variable is only replaced by the corresponding value if its value is set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants