using dotenv
npm's official documentation introduces dotenv like this: Dotenv is a zero-dependency module that loads variables in environment variables from a .env file into process.env.
use
- Install dotenv in the project
npm install dotenv
- Create a .env file in the root directory
HOST=localhost
PORT=3000
- Introduce dotenv under index.js in the root directory and use
require('dotenv').config({ path: '.env' })
// 使用
console.log(process.env.HOST) // localhost
console.log(process.env.PORT) // 3000
Using dotenv can save us from introducing configuration files in each file, and can also solve the leakage of sensitive information very well, which is conducive to post-code maintenance. Use it quickly!
Author: Front-end Xiaobai's digging and rolling
link: https://www.jianshu.com/p/16fa200234e9
Source: Jianshu
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.