A utility to convert JSON objects into environment variables.
This tool will flatten the JSON object and convert it into uppercase environment variables, setting the values in the process.env
object and optionally creating a .env
file.
Install the package via npm:
npm install json-to-env-converter
import { jsonToEnv } from 'json-to-env-converter';
const config = {
database: {
host: 'localhost',
port: 5432,
},
};
// Convert JSON to environment variables
const result = jsonToEnv(config);
console.log(process.env.DATABASE_HOST); // Output: 'localhost'
console.log(process.env.DATABASE_PORT); // Output: '5432'
console.log(result); // Output: { DATABASE_HOST: 'localhost', DATABASE_PORT: '5432' }
You can add a custom prefix to the environment variable names:
import { jsonToEnv } from 'json-to-env-converter';
const config = {
api: {
key: 'my-api-key',
secret: 'my-secret-key',
},
};
const result = jsonToEnv(config, { prefix: 'APP_' });
console.log(process.env.APP_API_KEY); // Output: 'my-api-key'
console.log(process.env.APP_API_SECRET); // Output: 'my-secret-key'
console.log(result); // Output: { APP_API_KEY: 'my-api-key', APP_API_SECRET: 'my-secret-key' }
The package automatically flattens nested objects into uppercase environment variables:
import { jsonToEnv } from 'json-to-env-converter';
const config = {
app: {
database: {
host: 'db-host',
port: 3306,
},
api: {
key: "my-api-key",
secret: "my-secret-key"
}
},
};
jsonToEnv(config);
console.log(process.env.APP_DATABASE_HOST); // Output: 'db-host'
console.log(process.env.APP_DATABASE_PORT); // Output: '3306'
console.log(process.env.APP_API_KEY); // Output: 'my-api-key'
console.log(process.env.APP_API_SECRET); // Output: 'my-secret-key'
The jsonToEnv
function accepts an optional options object with the following properties:
prefix
: A string to add a prefix to the environment variable names (default:''
).envFile
: A boolean to enable or disable the creation of the.env
file (default:false
); if enabled and the file already exists, it will be overwritten.envFileName
: A string to specify the name of the.env
file (default:.env
).envFilePath
: A string to specify the path where the.env
file should be created (default:.
).
import { jsonToEnv } from 'json-to-env-converter';
const config = {
database: {
host: 'localhost',
port: 5432,
},
api: {
key: 'my-api-key',
retries: 3,
},
enableFeatureX: true,
};
// Convert JSON to environment variables and create a .env file
jsonToEnv(config, { prefix: 'APP_', envFile: true, envFileName: '.env.local', envFilePath: './src' });
console.log(process.env.APP_DATABASE_HOST); // "localhost"
console.log(process.env.APP_DATABASE_PORT); // "5432"
console.log(process.env.APP_API_KEY); // "my-api-key"
console.log(process.env.APP_ENABLEFEATUREX); // "true"
If the input JSON is invalid, the function throws an error:
import { jsonToEnv } from 'json-to-env-converter';
try {
jsonToEnv([]);
} catch (error) {
console.error(error.message); // Output: 'Invalid input: Only JSON objects are supported.'
}
This project is licensed under the MIT License.