Node.js应用集成dotenvx
首先使用dotenvx命令行在目录下创建对应的.env
和.env.keys
文件,命令如下:
$ dotenvx init
$ dotenvx set HELLO "World"
$ dotenvx encrypt
安装dotenvx npm,命令如下:
$ npm install @dotenvx/dotenvx
创建index.js
文件,代码如下:
require('@dotenvx/dotenvx').config()
// or import '@dotenvx/dotenvx/config' if you're using esm
console.log(`Hello ${process.env.HELLO}`)
Run the application:
$ node index.js
即可看到对应的输出结果:Hello World
。
如果要在服务器或者其他环境中运行该应用,请设置DOTENV_PRIVATE_KEY
环境变量,其值来之于.env.keys
文件。