.env · generator · Node.js

Node.js .env Generator

Every Node.js service starts with a .env file. This generator produces one for Express, Fastify, or Koa — with a Postgres connection string, a securely generated JWT secret, and a log-level knob.

.env · Node.js · 5 keys
Customize →
NODE_ENV=development
PORT=3000
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
JWT_SECRET=Q1iSAb44DY3Lzbb7gh1bG6JD8GdZ52FT1yrjwtWCFPuzowq1mNjR4GJtkKdXHk1C
LOG_LEVEL=info

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in Node.js

  1. 1.Copy the .env into your project root.
  2. 2.Add .env and .env.local to .gitignore.
  3. 3.Install dotenv: npm install dotenv (or use Node 20+ --env-file=.env).
  4. 4.Import as the first line: import 'dotenv/config'.
  5. 5.Read anywhere: process.env.DATABASE_URL.

Gotchas

  • ⚠Don't call dotenv.config() after your DB connection code — env vars will be undefined.
  • ⚠Node 20+ has a built-in --env-file flag; you may not need the dotenv package at all.
  • ⚠Production hosts (Vercel, Railway, Fly) inject env vars via the dashboard — don't ship your .env.

Common keys explained

NODE_ENV

Runtime mode — development | production | test.

PORT

HTTP port the server binds to. Use 0 to let the OS pick in tests.

DATABASE_URL

Postgres/MySQL connection string.

JWT_SECRET

Secret used to sign access tokens. Always generate — never type.

LOG_LEVEL

One of trace | debug | info | warn | error. Default info.

Loading and validating the .env in Node.js

src/config.js
// Node 20.6+: node --env-file=.env src/server.js  (no package needed)
// Older Node: npm i dotenv, then load it before anything else
import "dotenv/config";

const required = ["DATABASE_URL", "JWT_SECRET"];
const missing = required.filter((k) => !process.env[k]);
if (missing.length) {
  throw new Error(`Missing env vars: ${missing.join(", ")}`);
}

export const config = {
  port: Number(process.env.PORT ?? 3000),
  databaseUrl: process.env.DATABASE_URL,
  jwtSecret: process.env.JWT_SECRET,
  logLevel: process.env.LOG_LEVEL ?? "info",
};

Node.js: env vars in development, staging and production

Locally, the .env file is the source of truth. In CI, set the same keys as pipeline secrets and start Node with plain environment variables: dotenv never overrides a variable that is already set, so CI values win over a stray .env.

In production, set NODE_ENV=production. Express, many ORMs and loggers switch to faster, less verbose code paths when they see it. Leaving it at development in production is a common cause of slow responses and stack traces leaking to users.

Node.js: .env errors and fixes

process.env.DATABASE_URL is undefined

dotenv was loaded after the module that reads it. Move import "dotenv/config" to the very first line of your entry file, or use node --env-file.

secretOrPrivateKey must have a value

jsonwebtoken got an empty JWT_SECRET. The key is missing from the .env the process actually read. Log process.cwd(): dotenv looks for .env in the working directory, not next to the script.

Error: listen EADDRINUSE :::3000

Another process already uses PORT. Change PORT in .env or stop the other process (lsof -i :3000).

Node.js .env FAQ

Do I still need the dotenv package in Node.js?

Not on Node 20.6 or newer: node --env-file=.env app.js loads the file natively, and Node 21.7+ adds process.loadEnvFile(). Keep dotenv if you need variable expansion (dotenv-expand) or support for older Node versions.

Should NODE_ENV be in my .env file?

It is fine locally, but set it in the host's environment in production. Some tools read NODE_ENV before your code runs and loads the .env.

How long should JWT_SECRET be?

At least 32 random bytes (64 hex characters) for HS256. The generator above creates 48 random bytes (64 base64url characters) on every load. Never reuse the same secret across staging and production.

Related tools

Other stacks