generate-certs
TypeScript icon, indicating that this package has built-in type declarations

2.1.0Β β€’Β PublicΒ β€’Β Published
banner

generate-certs

Effortless HTTPS certificate generation
for local development environments.

License npm version npm downloads stars

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';

// 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?

  • 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!

Package Sidebar

Install

npm i generate-certs

Weekly Downloads

252

Version

2.1.0

License

MIT

Unpacked Size

30.7 kB

Total Files

8

Last publish

Collaborators

  • wolfieleader