Read environment variables in Node.js

前端 未结 6 2071
不知归路
不知归路 2020-11-22 11:41

Is there a way to read environment variables in Node.js code?

Like for example Python\'s os.environ[\'HOME\'].

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 12:26

    You can use env package to manage your environment variables per project:

    • Create a .env file under the project directory and put all of your variables there.
    • Add this line in the top of your application entry file:
      require('dotenv').config();

    Done. Now you can access your environment variables with process.env.ENV_NAME.

提交回复
热议问题