xxxxxxxxxx
Node latest .env file update
with the latest update in node version if you add this, then no requirement to
add this line "require("dotenv").config();"
"script":{
"dev": "node --env-file=.env index.js"
}
index.js
----
console.log(process.env.mySecrets);
xxxxxxxxxx
First of all run this in your terminal
npm i dotenv
then on the top of your main file
require("dotenv").config();
and then u can use your env vars like
process.env.DISCORD_JS_BOT_TOKEN
xxxxxxxxxx
// .env
your_args = your_secret
// your_file.js
require("dotenv").config();
const your_args = process.env.your_args;
// OR
const your_args = process.env["your_args"];
xxxxxxxxxx
const fs = require("fs");
const os = require("os");
function setEnvValue(key, value) {
// read file from hdd & split if from a linebreak to a array
const ENV_VARS = fs.readFileSync("./.env", "utf8").split(os.EOL);
// find the env we want based on the key
const target = ENV_VARS.indexOf(ENV_VARS.find((line) => {
return line.match(new RegExp(key));
}));
// replace the key/value with the new value
ENV_VARS.splice(target, 1, `${key}=${value}`);
// write everything back to the file system
fs.writeFileSync("./.env", ENV_VARS.join(os.EOL));
}
setEnvValue("VAR1", "ENV_1_VAL");
xxxxxxxxxx
NODE_ENV=development
PORT=8626
# Set your database/API connection information here
API_KEY=**************************
API_URL=**************************
xxxxxxxxxx
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
xxxxxxxxxx
const fs = require("fs");
const os = require("os");
const path = require("path");
const envFilePath = path.resolve(__dirname, ".env");
// read .env file & convert to array
const readEnvVars = () => fs.readFileSync(envFilePath, "utf-8").split(os.EOL);
/**
* Finds the key in .env files and returns the corresponding value
*
* @param {string} key Key to find
* @returns {string|null} Value of the key
*/
const getEnvValue = (key) => {
// find the line that contains the key (exact match)
const matchedLine = readEnvVars().find((line) => line.split("=")[0] === key);
// split the line (delimiter is '=') and return the item at index 2
return matchedLine !== undefined ? matchedLine.split("=")[1] : null;
};
/**
* Updates value for existing key or creates a new key=value line
*
* This function is a modified version of https://stackoverflow.com/a/65001580/3153583
*
* @param {string} key Key to update/insert
* @param {string} value Value to update/insert
*/
const setEnvValue = (key, value) => {
const envVars = readEnvVars();
const targetLine = envVars.find((line) => line.split("=")[0] === key);
if (targetLine !== undefined) {
// update existing line
const targetLineIndex = envVars.indexOf(targetLine);
// replace the key/value with the new value
envVars.splice(targetLineIndex, 1, `${key}="${value}"`);
} else {
// create new key value
envVars.push(`${key}="${value}"`);
}
// write everything back to the file system
fs.writeFileSync(envFilePath, envVars.join(os.EOL));
};
// examples
console.log(getEnvValue('KEY_1'));
setEnvValue('KEY_1', 'value 1')