process.env在运行IOS/Android时无法找到终端环境变量。

huangapple go评论64阅读模式
英文:

process.env can't find terminal environmental variables when running IOS/Android

问题

Variables defined in the .env file are not showing up in process.env (in babel.config.js as well as in index.tsx).

.env 文件中定义的变量未显示在 process.env 中(在 babel.config.jsindex.tsx 中)。

babel.config:

console.log(process.env);
module.exports = {
    // ...
}

终端输出:

user@Users-MacBook-Pro react_frontend % yarn start --reset-cache   
warning:  Obfuscation SKIPPED [Not a *bundle* command]

                        ▒▒▓▓▓▓▒▒
                     ▒▓▓▓▒▒░░▒▒▓▓▓▒
                  ▒▓▓▓▓░░░▒▒▒▒░░░▓▓▓▓▒
                 ▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓
                 ▓▓░░░░░▒▓▓▓▓▓▓▒░░░░░▓▓
                 ▓▓░░▓▓▒░░░▒▒░░░▒▓▒░░▓▓
                 ▓▓░░▓▓▓▓▓▒▒▒▒▓▓▓▓▒░░▓▓
                 ▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░▓▓
                 ▓▓▒░░▒▒▓▓▓▓▓▓▓▓▒░░░▒▓▓
                  ▒▓▓▓▒░░░▒▓▓▒░░░▒▓▓▓▒
                     ▒▓▓▓▒░░░░▒▓▓▓▒
                        ▒▒▓▓▓▓▒▒

warning: the transform cache was reset.
                Welcome to Metro v0.72.3
              Fast - Scalable - Integrated

To reload the app press "r"
To open developer menu press "d"
user@Users-MacBook-Pro react_frontend % source .env && echo $ENV_VAR1 && npx react-native run-android
testabc
info Starting JS server...
info Installing the app...

Metro终端:

...
transform[stdout]: {
transform[stdout]:   MANPATH: '/opt/homebrew/share/man::',
transform[stdout]:   rvm_bin_path: '/Users/xxxxxx/.rvm/bin',
transform[stdout]:   VIRTUALENVWRAPPER_SCRIPT: '/opt/homebrew/bin/virtualenvwrapper.sh',
transform[stdout]:   VIRTUALENVWRAPPER_PROJECT_FILENAME: '.project',
transform[stdout]:   TERM_PROGRAM: 'Apple_Terminal',
transform[stdout]:   GEM_HOME: '/Users/xxxxxx/.rvm/gems/ruby-3.0.0',
transform[stdout]:   ANDROID_HOME: '/Users/xxxxx/Library/Android/sdk',
transform[stdout]:   SHELL: '/bin/zsh',
transform[stdout]:   TERM: 'xterm-256color',
transform[stdout]:   IRBRC: '/Users/xxxxxxx/.rvm/rubies/ruby-3.0.0/.irbrc',
transform[stdout]:   HOMEBREW_REPOSITORY: '/opt/homebrew',
transform[stdout]:   TMPDIR: '/var/folders/nq/_9bt81g17cj0036184j_gxrw0000gn/T/',
transform[stdout]:   TERM_PROGRAM_VERSION: '447',
transform[stdout]:   MY_RUBY_HOME: '/Users/xxxx/.rvm/rubies/ruby-3.0.0',
transform[stdout]:   TERM_SESSION_ID: 'xxxxxx',
transform[stdout]:   JENV_FORCEJAVAHOME: 'true',
transform[stdout]:   USER: 'xxxxx',
transform[stdout]:   rvm_path: '/Users/xxxxxx/.rvm',
transform[stdout]:   SSH_AUTH_SOCK: '/xxxxxx',
transform[stdout]:   PODS_ROOT: '/xxxxx/node_modules/react-native/scripts/../../../ios/Pods',
transform[stdout]:   WORKON_HOME: '/Users/xxxxx/.virtualenvs',
transform[stdout]:   rvm_prefix: '/Users/xxxxxx',
transform[stdout]:   VIRTUALENVWRAPPER_PYTHON: '/opt/homebrew/bin/python3.10',
transform[stdout]:   PATH: 'xxxxxxx',
transform[stdout]:   VIRTUALENVWRAPPER_HOOK_DIR: '/Users/xxxxxx/.virtualenvs',
transform[stdout]:   __CFBundleIdentifier: 'com.apple.Terminal',
transform[stdout]:   JENV_LOADED: '1',
transform[stdout]:   PWD: '/Users/xxxx/Desktop/xxxx/xxxx/xxxxx',
transform[stdout]:   JAVA_HOME: '/Users/xxxxx/.jenv/versions/11.0.17',
transform[stdout]:   XPC_FLAGS: '0x0',
transform[stdout]:   VIRTUALENVWRAPPER_VIRTUALENV: '/opt/homebrew/bin/virtualenv',
transform[stdout]:   RCT_METRO_PORT: '8081',
transform[stdout]:   JENV_FORCEJDKHOME: 'true',
transform[stdout]:   XPC_SERVICE_NAME: 'xxxxx',
transform[stdout]:   rvm_version: '1.29.12 (latest)',
transform[stdout]:   JDK_HOME: '/Users/xxxxx/.jenv/versions/11.0.17',
transform[stdout]:   SHLVL: '2',
transform[stdout]:   HOME: '/Users/xxxxxx',
transform[stdout]:   HOMEBREW_PREFIX: '/opt/homebrew',
transform[stdout]:   LOGNAME: 'xxxxxxx',
transform[stdout]:   JENV_SHELL: 'zsh',
transform[stdout]:   LC_CTYPE: 'UTF-8',
transform[stdout]:   GEM_PATH: '/Users/xxxxxx/.rvm/gems/ruby-3.0.0:/Users/xxxxxxx/.rvm/gems/ruby-3.0.0@global',
transform[stdout]:   NODE_BINARY: '/opt/homebrew/Cellar/node/19.4.0_1/bin/node',
transform[stdout]:   VIRTUALENVWRAPPER_WORKON_CD: '1',
transform[stdout]:   INFOPATH: '/opt/homebrew/share/info:',
transform[stdout]:   HOMEBREW_CELLAR: '/opt/homebrew/Cellar',
transform[stdout]:   RUBY_VERSION: 'ruby-3.0

<details>
<summary>英文:</summary>

Variables defined in .env file are not showing up in process.env (in `babel.config.js` as well as in `index.tsx`)

`.env`:

ENV_VAR1=testabc
ENV_VAR2=testdef

`babel.config`:

console.log(process.env);
module.exports = {
....

Terminal:

user@Users-MacBook-Pro react_frontend % yarn start --reset-cache
warning: Obfuscation SKIPPED [Not a bundle command]

                    ▒▒▓▓▓▓▒▒
▒▓▓▓▒▒░░▒▒▓▓▓▒
▒▓▓▓▓░░░▒▒▒▒░░░▓▓▓▓▒
▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓
▓▓░░░░░▒▓▓▓▓▓▓▒░░░░░▓▓
▓▓░░▓▓▒░░░▒▒░░░▒▓▒░░▓▓
▓▓░░▓▓▓▓▓▒▒▒▒▓▓▓▓▒░░▓▓
▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░▓▓
▓▓▒░░▒▒▓▓▓▓▓▓▓▓▒░░░▒▓▓
▒▓▓▓▒░░░▒▓▓▒░░░▒▓▓▓▒
▒▓▓▓▒░░░░▒▓▓▓▒
▒▒▓▓▓▓▒▒

warning: the transform cache was reset.
Welcome to Metro v0.72.3
Fast - Scalable - Integrated

To reload the app press "r"
To open developer menu press "d"
user@Users-MacBook-Pro react_frontend % source .env && echo $ENV_VAR1 && npx react-native run-android
testabc
info Starting JS server...
info Installing the app...

Metro terminal:

...
transform[stdout]: {
transform[stdout]: MANPATH: '/opt/homebrew/share/man::',
transform[stdout]: rvm_bin_path: '/Users/xxxxxx/.rvm/bin',
transform[stdout]: VIRTUALENVWRAPPER_SCRIPT: '/opt/homebrew/bin/virtualenvwrapper.sh',
transform[stdout]: VIRTUALENVWRAPPER_PROJECT_FILENAME: '.project',
transform[stdout]: TERM_PROGRAM: 'Apple_Terminal',
transform[stdout]: GEM_HOME: '/Users/xxxxxx/.rvm/gems/ruby-3.0.0',
transform[stdout]: ANDROID_HOME: '/Users/xxxxx/Library/Android/sdk',
transform[stdout]: SHELL: '/bin/zsh',
transform[stdout]: TERM: 'xterm-256color',
transform[stdout]: IRBRC: '/Users/xxxxxxx/.rvm/rubies/ruby-3.0.0/.irbrc',
transform[stdout]: HOMEBREW_REPOSITORY: '/opt/homebrew',
transform[stdout]: TMPDIR: '/var/folders/nq/_9bt81g17cj0036184j_gxrw0000gn/T/',
transform[stdout]: TERM_PROGRAM_VERSION: '447',
transform[stdout]: MY_RUBY_HOME: '/Users/xxxx/.rvm/rubies/ruby-3.0.0',
transform[stdout]: TERM_SESSION_ID: 'xxxxxx',
transform[stdout]: JENV_FORCEJAVAHOME: 'true',
transform[stdout]: USER: 'xxxxx',
transform[stdout]: rvm_path: '/Users/xxxxxx/.rvm',
transform[stdout]: SSH_AUTH_SOCK: '/xxxxxx',
transform[stdout]: PODS_ROOT: '/xxxxx/node_modules/react-native/scripts/../../../ios/Pods',
transform[stdout]: WORKON_HOME: '/Users/xxxxx/.virtualenvs',
transform[stdout]: rvm_prefix: '/Users/xxxxxx',
transform[stdout]: VIRTUALENVWRAPPER_PYTHON: '/opt/homebrew/bin/python3.10',
transform[stdout]: PATH: 'xxxxxxx',
transform[stdout]: VIRTUALENVWRAPPER_HOOK_DIR: '/Users/xxxxxx/.virtualenvs',
transform[stdout]: __CFBundleIdentifier: 'com.apple.Terminal',
transform[stdout]: JENV_LOADED: '1',
transform[stdout]: PWD: '/Users/xxxx/Desktop/xxxx/xxxx/xxxxx',
transform[stdout]: JAVA_HOME: '/Users/xxxxx/.jenv/versions/11.0.17',
transform[stdout]: XPC_FLAGS: '0x0',
transform[stdout]: VIRTUALENVWRAPPER_VIRTUALENV: '/opt/homebrew/bin/virtualenv',
transform[stdout]: RCT_METRO_PORT: '8081',
transform[stdout]: JENV_FORCEJDKHOME: 'true',
transform[stdout]: XPC_SERVICE_NAME: 'xxxxx',
transform[stdout]: rvm_version: '1.29.12 (latest)',
transform[stdout]: JDK_HOME: '/Users/xxxxx/.jenv/versions/11.0.17',
transform[stdout]: SHLVL: '2',
transform[stdout]: HOME: '/Users/xxxxxx',
transform[stdout]: HOMEBREW_PREFIX: '/opt/homebrew',
transform[stdout]: LOGNAME: 'xxxxxxx',
transform[stdout]: JENV_SHELL: 'zsh',
transform[stdout]: LC_CTYPE: 'UTF-8',
transform[stdout]: GEM_PATH: '/Users/xxxxxx/.rvm/gems/ruby-3.0.0:/Users/xxxxxxx/.rvm/gems/ruby-3.0.0@global',
transform[stdout]: NODE_BINARY: '/opt/homebrew/Cellar/node/19.4.0_1/bin/node',
transform[stdout]: VIRTUALENVWRAPPER_WORKON_CD: '1',
transform[stdout]: INFOPATH: '/opt/homebrew/share/info:',
transform[stdout]: HOMEBREW_CELLAR: '/opt/homebrew/Cellar',
transform[stdout]: RUBY_VERSION: 'ruby-3.0.0',
transform[stdout]: OLDPWD: '/Users/xxxx',
transform[stdout]: _: '/opt/homebrew/Cellar/node/19.4.0_1/bin/node',
transform[stdout]: __CF_USER_TEXT_ENCODING: 'xxxxxx',
transform[stdout]: FORCE_COLOR: '1',
transform[stdout]: BABEL_ENV: 'xxxxxx'
transform[stdout]: }


</details>
# 答案1
**得分**: 0
`yarn add dotenv --dev`
`babel.config`文件:

require('dotenv').config();
console.log(process.env);
module.exports = {
....

答案:[https://stackoverflow.com/a/61442921/9611924](https://stackoverflow.com/a/61442921/9611924)
<details>
<summary>英文:</summary>
fix:
`yarn add dotenv --dev`
`babel.config` file:

require('dotenv').config();
console.log(process.env);
module.exports = {
....

answer: https://stackoverflow.com/a/61442921/9611924
</details>

huangapple
  • 本文由 发表于 2023年2月20日 00:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定