About π
generate-certs
is a simple and developer-friendly utility for generating self-signed HTTPS certificates during local development.
It streamlines the process of creating key.pem
and cert.pem
files, supports both CommonJS and ES Modules, and integrates seamlessly into frameworks like Express and NestJS.
Features π‘
- π 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.
- βοΈ Type-Safe & Cross-Compatible β Fully written in TypeScript with native types. Works in both ESM and CommonJS runtimes.
Installation π₯
npm install -D generate-certs@latest
π‘ Works with npm
, pnpm
, and yarn
. You can use it in dev dependencies since it's typically used only for local HTTPS.
Usage πͺ
Basic Example π£
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { generateCerts } from 'generate-certs';
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';
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?
- 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
License π
This project is licensed under the MIT License.
Thank you!