generate-certs
v3.0.0
Published
ποΈ Effortless HTTPS Certificate Generation for Local Development
Maintainers
Readme
Not for production. These are self-signed certificates for local development only. For production, use a trusted CA like Let's Encrypt.
Highlights β¨
- Automatic certificate generation β no OpenSSL needed
- Intelligent reuse β validates expiry, permissions, and key/cert pairing
- Pre-configured for localhost β
127.0.0.1,::1, andlocalhostSANs - Framework-ready β Express, NestJS, Hono, Fastify
- Secure file permissions enforced (Unix:
600on private key)
Installation π¦
Requires Node.js >= 18. Recommended as a dev dependency.
npm install -D generate-certs
# or
pnpm add -D generate-certsQuick Start π
import path from "node:path";
import https from "node:https";
import express from "express";
import { generateCerts } from "generate-certs";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
const app = express();
https.createServer(certs, app).listen(3443);API Reference π
generateCerts(options): { key: string; cert: string }
Generates or retrieves self-signed certificates from the specified directory. If valid certificates already exist, they are reused. Returns { key: string; cert: string } as PEM-formatted strings, ready for https.createServer().
import path from "node:path";
import { generateCerts } from "generate-certs";
// Auto-generates key.pem and cert.pem (or reuses valid existing ones)
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
console.log(certs.key); // "-----BEGIN RSA PRIVATE KEY-----\n..."
console.log(certs.cert); // "-----BEGIN CERTIFICATE-----\n..."
// Suppress console logs in CI/test environments
const silent = generateCerts({
certsPath: path.resolve(import.meta.dirname, "../certs"),
activateLogs: false,
});Options: certsPath (required, absolute path to certificates directory), activateLogs (default true).
Throws if the path is invalid, inaccessible, or certificate generation fails.
Certificate Details π
| Property | Value |
| ----------------------- | ------------------------------- |
| Key algorithm | RSA 2048-bit |
| Signature | SHA-256 |
| Validity | 1 year from generation |
| Common Name | localhost |
| Subject Alt Names | localhost, 127.0.0.1, ::1 |
| Private key permissions | 600 (Unix only) |
Smart Reuse β»οΈ
When certificates already exist at the specified path, the following checks are performed before reusing them:
- File existence β both
key.pemandcert.pemmust be present - Permissions β private key must have
600permissions (Unix only, skipped on Windows) - Expiry β certificate must not be expired or expiring within 5 minutes
- Not-before β certificate must already be valid (not issued for the future)
- Common Name β must be
localhost - SANs β must include
localhost,127.0.0.1, and::1 - Key size β RSA key must be at least 2048-bit
- Key/cert pairing β the private key must match the certificate's public key
If any check fails, certificates are automatically regenerated.
Framework Examples π§©
NestJS
import path from "node:path";
import { NestFactory } from "@nestjs/core";
import { generateCerts } from "generate-certs";
import { AppModule } from "./app.module";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
async function bootstrap() {
const app = await NestFactory.create(AppModule, { httpsOptions: certs });
await app.listen(3443);
}
bootstrap();HonoJS
import { createSecureServer } from "node:http2";
import path from "node:path";
import { serve } from "@hono/node-server";
import { generateCerts } from "generate-certs";
import { Hono } from "hono";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
const app = new Hono();
serve({
fetch: app.fetch,
port: 3443,
createServer: createSecureServer,
serverOptions: certs,
});Fastify
import path from "node:path";
import Fastify from "fastify";
import { generateCerts } from "generate-certs";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
async function bootstrap() {
const app = Fastify({ https: certs });
await app.listen({ port: 3443, host: "0.0.0.0" });
}
bootstrap();Best Practices β
- Add
certs/to.gitignoreβ never commit generated certificates to version control - Install as a dev dependency (
-D) β this package is not needed in production - Browser warnings are expected β self-signed certificates will show "Not Secure"; click Advanced β Proceed to localhost
- Suppress logs with
activateLogs: falsewhen running in CI or test environments
Windows: File permission checks are skipped on Windows. Lock file stale detection uses a 60-second timeout as a cross-platform fallback.
Type Exports π·οΈ
import type { GenerateCertsOptions } from "generate-certs";Credits π
Built on node-forge for RSA key generation and X.509 certificate creation.
Contributions π€
- Open an issue or feature request
- Submit a PR to improve the package
- Star the repo if you find it useful
Crafted carefully by WolfieLeader
This project is licensed under the MIT License.
