env-creator
v1.2.4
Published
A fast, lightweight CLI tool for managing and generating custom environment (`.env`) files
Maintainers
Readme
env-creator
A fast, lightweight CLI tool for managing and generating custom environment (.env) files, designed to simplify development by enabling multiple .env configurations for different environments, as well as automatically generating JavaScript constants for direct use within the project.
Installation
You can install env-creator locally in your project (recommended) or globally.
Local Installation
Install as a development dependency:
npm install env-creator --save-dev
# or
pnpm add -D env-creator
# or
yarn add -D env-creatorThen run it using your package manager's runner:
npx env-creator create
# or
pnpx env-creator create
# or (Yarn Berry)
yarn dlx env-creator create
# or (Yarn Classic)
yarn env-creator createGlobal Installation
If you prefer to use it across all projects:
npm install -g env-creator
# or
pnpm add -g env-creator
# or
yarn global add env-creatorThen run it directly:
env-creator createUsage (examples for pnpm)
env-creator (also available as env) provides several commands (along with their short aliases, e.g. c for create) to help you quickly set up or split your environment files.
Commands:
- 1. Create an empty or pre-filled
.envfile - 2. Create from JSON
- 3. Split
.envfor specific environments - 4. Sort
.envkeys alphabetically - 5. Delete an
.envfile - 6. Generate environment constants
1. Create an empty or pre-filled .env file (alias: c)
Generates a .env file in the current working directory. You can optionally pass KEY=VALUE pairs to pre-fill it. If a file already exists, it will not overwrite it.
pnpx env-creator createWith initial fields:
pnpx env-creator create PORT=3000 NODE_ENV=development
# or short version
pnpm exec env c PORT=3000 NODE_ENV=developmentResulting .env:
PORT=3000
NODE_ENV=development2. Create from JSON (alias: cfj)
Generates a .env target file from a provided JSON file. The keys and values in the JSON file will be converted into KEY=VALUE format.
You can optionally provide an --env <name> flag to create a specific .env.<name> file (e.g., .env.production).
pnpx env-creator create-from-json <path-to-file.json> [--env <name>]Example config.json:
{
"PORT": 3000,
"DB_HOST": "localhost",
"NODE_ENV": "development"
}Command:
pnpx env-creator create-from-json config.json --env productionResulting .env.production:
PORT=3000
DB_HOST=localhost
NODE_ENV=development3. Split .env for specific environments (alias: s)
Reads your existing .env file, removes all comments and values, and creates a new target file containing only the keys (e.g., for creating a .env.example or .env.production template).
pnpx env-creator split --env <env-name>Example:
If you have a .env file like this:
# Database Config
DB_USER=admin
DB_PASS=secretRunning the split command for production:
pnpx env-creator split --env productionWill generate a new file named .env.production with empty values:
DB_USER=
DB_PASS=4. Sort .env keys alphabetically (alias: srt)
Reads an environment file and reorders all KEY=VALUE lines alphabetically. By default, it operates in a flat sort mode, meaning it binds each comment or empty line to the closest variable immediately below it, and sorts these blocks of keys.
If you prefer to strictly preserve the exact file layout and structure, use the --groups (or -g) flag, which performs intra-group sorting that alphabetizes variables tightly inside their original continuous groups. Defaults to .env if no file is specified.
pnpx env-creator sort [--groups] [file]Examples:
pnpx env-creator sort # Flat sorts .env
pnpx env-creator sort -g .env.production # Group-sorts .env.productionExample 1: Flat Sort (Default)
Before:
DB_USER=admin
DB_PASS=secret
PORT=3000
APP_NAME=my-appAfter (env-creator sort):
APP_NAME=my-app
DB_PASS=secret
DB_USER=admin
PORT=3000Example 2: Intra-Group Sort (--groups)
Before:
# DB Config
DB_USER=admin
DB_PASS=secret
# App
PORT=3000
APP_NAME=my-appAfter (env-creator sort -g):
# DB Config
DB_PASS=secret
DB_USER=admin
# App
APP_NAME=my-app
PORT=30005. Delete an .env file (alias: d)
Deletes a specific environment file. If no filename is provided, it defaults to deleting .env.
pnpx env-creator delete [file]Examples:
pnpx env-creator delete # Deletes .env
pnpx env-creator delete .env.production # Deletes .env.production6. Generate environment constants (alias: gc)
Reads an environment file (defaults to .env), extracts the keys, and creates a JavaScript file exporting each key inside a constant object (defaults to envConstants.js). You can specify a custom output file using the --out flag.
pnpx env-creator generate-constants [file] [--out <filename>]Examples:
pnpm exec env gc # Generates envConstants.js from .env by default
pnpm exec env gc .env.production # Generates envConstants.js from .env.production
pnpm exec env gc --out myConfig.js # Generates myConfig.js from .env
pnpm exec env gc .env.production --out config/env.js # Generates config/env.js from .env.productionResulting envConstants.js:
export const ENV = {
API_URL: process.env.API_URL,
JWT_SECRET: process.env.JWT_SECRET,
};[!IMPORTANT] There is no
processobject in the browser. To use these constants on the client-side, you must inject the environment variables using your bundler.Example for Webpack (other bundlers like Vite or Rollup will require their own specific configuration):
import webpack from "webpack"; import dotenv from "dotenv"; import path from "path"; // if needed // load environment variables from .env const env = dotenv.config().parsed; // load environment variables from a specific .env file (for example, .env.production) // const env = dotenv.config({ path: path.resolve(__dirname, `../.env.${envName}`) }).parsed; // convert to an object for DefinePlugin const envKeys = Object.keys(env).reduce((acc, key) => { acc[`process.env.${key}`] = JSON.stringify(env[key]); return acc; }, {}); // inject variables passing them into DefinePlugin export default { // ... plugins: [ new webpack.DefinePlugin({ ...envKeys }), ], };
Now you can easily import these constants anywhere in your project:
import { ENV } from "[pathToFile]/envConstants";
console.log(`API URL: ${ENV.API_URL}`); // delete after checkingDevelopment
If you want to contribute or modify the tool, you can clone the repository and use the built-in npm scripts:
Linting
To check the code for syntax and style errors using ESLint:
pnpx run lintBuilding
The project uses esbuild to bundle and minify the code into a single executable file in the dist/ directory:
pnpx run buildLicense
This project is licensed under the MIT License - see the LICENSE file for details.
