Can't read my environment variable in my Node.js app

后端 未结 4 663
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-02 15:10

I am on Ubuntu 12.04 and I\'m just learning about environment variables. I am trying to read a custom variable from within my application but it always shows up as undefin

相关标签:
4条回答
  • 2021-02-02 15:19

    Restart your bash (source ~/.bashrc). This will take into account your system environment.

    0 讨论(0)
  • 2021-02-02 15:21

    I found my way here from something really silly.

    I had just added the new exported variables, but my node process still wasn't seeing them. Then I realized it wasn't enough to restart the node process—I had to open a new terminal (ie. bash instance) too. Once I did this, it worked fine :)

    0 讨论(0)
  • 2021-02-02 15:25

    You need to export shell variables in order to make them available to processes you execute in your shell.

    Compare the output of this command:

    FOO=bar; bash -c 'echo $FOO'
    

    with the output of this:

    export FOO=bar; bash -c 'echo $FOO'
    
    0 讨论(0)
  • 2021-02-02 15:40

    You might want to consider using a library for managing app configuration.

    For example nconf helps manage configuration through

    • command line argumets
    • environment variables
    • files
    • etc..

    And looking at the source is a nice way to learn https://github.com/flatiron/nconf

    0 讨论(0)
提交回复
热议问题