generate-certs
v2.1.1
Published
๐๏ธ Effortless HTTPS Certificate Generation for Local Development
Downloads
1,330
Maintainers
Readme
Why generate-certs? ๐ค
- ๐ Automatic Certificate Generation โ Creates valid self-signed certificates for
localhost. - ๐ Reusability โ Automatically detects and reuses existing certs if they exist.
- ๐งช Development-Ready โ Ideal for testing HTTPS locally without browser complaints.
- ๐ก Minimal Setup โ No OpenSSL or third-party installations required.
- ๐งฉ Framework Friendly โ Easily integrates with Express, NestJS, and other Node.js frameworks.
Installation ๐ฅ
npm install -D generate-certs@latest
# or
yarn add -D generate-certs@latest
# or
pnpm install -D generate-certs@latest
# or
bun add -d generate-certs@latestUsage ๐ช
Basic Example ๐ฃ
import path from "node:path";
import { fileURLToPath } from "node:url";
import { generateCerts } from "generate-certs";
// If you are using ESM do the following, otherwise you can skip this part
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const certs = generateCerts({ certsPath: path.resolve(__dirname, "../certs") });Express ๐ซ
import https from "node:https";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { generateCerts } from "generate-certs";
import express from "express";
import { env } from "./env";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const certs = generateCerts({ certsPath: path.resolve(__dirname, "../certs") });
function bootstrap() {
const app = express();
https.createServer(certs, app).listen(env.PORT || 3443, () => {
console.log(`๐ Express server running on: https://localhost:${env.PORT || 3443}`);
});
}
bootstrap();NestJS ๐ชบ
import path from "node:path";
import { NestFactory } from "@nestjs/core";
import { generateCerts } from "generate-certs";
import { AppModule } from "./app.module";
import { env } from "./env";
// NestJS commonly uses CommonJS, so you can skip the ESM import part
const certs = generateCerts({ certsPath: path.resolve(__dirname, "../certs") });
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
httpsOptions: certs,
});
await app.listen(env.SERVER_PORT || 3443);
console.log(`๐ NestJS server running on: https://localhost:${env.SERVER_PORT || 3443}`);
}
bootstrap();HonoJS ๐ฅ
import { createSecureServer } from "node:http2";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { serve } from "@hono/node-server";
import { generateCerts } from "generate-certs";
import { Hono } from "hono";
import { env } from "./env";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const certs = generateCerts({ certsPath: path.resolve(__dirname, "../certs") });
function bootstrap() {
const app = new Hono();
serve(
{
fetch: app.fetch,
port: env.PORT || 3443,
createServer: createSecureServer,
serverOptions: certs,
},
(info) => {
console.log(`๐ HonoJS server running on: https://localhost:${env.PORT || 3443}`);
},
);
}
bootstrap();Fastify โก
import path from "node:path";
import { fileURLToPath } from "node:url";
import Fastify from "fastify";
import { generateCerts } from "generate-certs";
import { env } from "./env";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const certs = generateCerts({ certsPath: path.resolve(__dirname, "../certs") });
async function bootstrap() {
const app = new Fastify({ https: certs });
await app.listen({ port: env.PORT || 3443, host: "0.0.0.0" });
console.log(`๐ Fastify server running on: https://localhost:${env.PORT || 3443}`);
}
bootstrap();Notesโ
- ๐งช First-Time Run: The certs are created automatically and stored in the provided folder.
- โ ๏ธ Browser Warnings: You may see โNot Secureโ warnings with self-signed certs โ click โAdvancedโ โ โProceed to localhost (unsafe)โ to continue.
- ๐ Not for Production: These are local dev certificates. For production, use certs from a trusted CA (like Let's Encrypt).
- ๐ Permissions: Ensure the target folder is writable and readable by your application.
Contributions ๐ค
Want to contribute or suggest a feature or improvement?
- Open an issue or feature request
- Submit a PR to improve the packages or add new ones
- Star โญ the repo if you like what you see
