This package converts your JSON file to environment variable format. This executable also provides process.env.<key>
for you to copy in your code.
npm install -g @syedbasim/jsenv
jsenv <input file> <output file> -w -p <?prefix>
Note: -w
(write) and -p <prefix>
(prefix) are optional. See Prefix and Append & Write modes for more detail.
file.json
{
"port": 8000,
"projectName": "json-to-env-var"
}
In Terminal:
jsenv file.json .env
Will output in .env
file:
PORT=8000
PROJECT_NAME="json-to-env-var"
Also, the terminal will generate code which you can copy and paste in your JS/TS files:
{
port: process.env.PUBLIC_VAR_PORT,
projectName: process.env.PUBLIC_VAR_PROJECT_NAME
}
jsenv file.json .env -p public_var
Will output to:
PUBLIC_VAR_PORT=8000
PUBLIC_VAR_PROJECT_NAME="json-to-env-var"
By default, this executable will append to the output file. If you want to write to it (i.e. remove any existing data in the output file and then writing), you can specify -w
flag.
jsenv file.json .env -w
jsenv file.json .env -w -p prefix