How to set process.env from the file in NodeJS?

前端 未结 2 1321
刺人心
刺人心 2020-12-19 02:27

I\'m new to the Node.JS. I found few articles says we can use .env file to setup the process.env variable, e.g.,

PORT = 8081

but when I run

相关标签:
2条回答
  • 2020-12-19 03:00

    Dotenv file have become the most popular mode to separate configuratione from app, using system environment variables (see 12factor config).

    On node there exists a lot of libraries for loading config from .env file. The most popular is motdotla/dotenv. You can read a lot of examples on readme file about the usage of this library

    0 讨论(0)
  • 2020-12-19 03:17

    Make a config.js file with the following content:

    module.exports = {
        bar: 'someValue',
        foo: 'otherValue'
        ...
    }
    

    Then you can do this in some file:

    const config = require('./config');
    let foo = config.foo;
    
    0 讨论(0)
提交回复
热议问题