react-native-dotenv icon indicating copy to clipboard operation
react-native-dotenv copied to clipboard

NODE_ENV is undefined

Open karlvr opened this issue 2 years ago • 11 comments

  • [x] Tried the troubleshooting Wiki
  • [x] 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.

karlvr avatar Nov 11 '21 06:11 karlvr

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

github-actions[bot] avatar Nov 11 '21 06:11 github-actions[bot]

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

goatandsheep avatar Nov 15 '21 02:11 goatandsheep

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?

tgwow avatar Dec 13 '21 14:12 tgwow

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

goatandsheep avatar Dec 13 '21 17:12 goatandsheep

@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.

tgwow avatar Dec 13 '21 17:12 tgwow

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 avatar Dec 13 '21 18:12 goatandsheep

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

goatandsheep avatar Dec 13 '21 18:12 goatandsheep

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
*/

allandiego avatar Mar 13 '22 23:03 allandiego

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)

douglascavanaugh avatar Jul 07 '22 00:07 douglascavanaugh

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

exoriri avatar Aug 19 '22 12:08 exoriri

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

github-actions[bot] avatar Sep 14 '22 23:09 github-actions[bot]