我dotenv
用于读取环境变量。像这样:
let dotenv = require('dotenv').config({ path: '../../.env' });
console.log(process.env.DB_HOST);
现在,我想将更改保存到.env
文件中。我找不到任何将变量保存在.env
文件中的方法。我该怎么办?
process.env.DB_HOST = '192.168.1.62';
.env文件
VAR1=var1Value
VAR_2=var2Value
index.js文件
const fs = require('fs')
const envfile = require('envfile')
const sourcePath = '.env'
console.log(envfile.parseFileSync(sourcePath))
let parsedFile = envfile.parseFileSync(sourcePath);
parsedFile.NEW_VAR = 'newVariableValue'
fs.writeFileSync('./.env', envfile.stringifySync(parsedFile))
console.log(envfile.stringifySync(parsedFile))
最终的.env文件安装所需的模块并执行index.js文件
VAR1=var1Value
VAR_2=var2Value
NEW_VAR=newVariableValue
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句