generate-certs

中文文档
4.0.0发布于 2026/11/04MITTypeScriptESM + CJS构建来源已验证

🗝️ Effortless HTTPS Certificate Generation for Local Development

原始使用文档
文档目录16 节
  1. Highlights ✨
  2. Installation 📦
  3. Quick Start 🚀
  4. API Reference 📖
  5. generateCerts(options): { key: string; cert: string }
  6. Certificate Details 📜
  7. Smart Reuse ♻️
  8. Framework Examples 🧩
  9. NestJS
  10. HonoJS
  11. Fastify
  12. Best Practices ✅
  13. Breaking Changes in v4 ⚠️
  14. Type Exports 🏷️
  15. Credits 🙏
  16. Contributions 🤝
banner

generate-certs

Self-signed HTTPS certificates for local development.
Auto-generates, validates, and reuses.

License npm version npm downloads stars

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, and localhost SANs
  • Framework-ready — Express, NestJS, Hono, Fastify
  • Secure file permissions enforced (Unix: 600 on private key)

Installation 📦

Requires Node.js >= 22.12. Recommended as a dev dependency.

npm install -D generate-certs
# or
pnpm add -D generate-certs

Quick 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..."

// Print what happened (reuse, permission fix, backup, generation)
const logged = generateCerts({
  certsPath: path.resolve(import.meta.dirname, "../certs"),
  logger: console.log,
});

Options: certsPath (required, absolute path to the certificates directory), logger (optional (message: string) => void; silent when omitted).

Throws if the path is invalid, inaccessible, or certificate generation fails.

Certificate Details 📜

PropertyValue
Key algorithmRSA 2048-bit
SignatureSHA-256
Validity1 year from generation
Common Namelocalhost
Subject Alt Nameslocalhost, 127.0.0.1, ::1
Basic ConstraintsCA:FALSE (critical)
Key UsageDigital Signature, Key Encipherment (critical)
Extended Key UsageTLS Web Server Authentication
Private key permissions600 (Unix only)

Smart Reuse ♻️

Existing key.pem and cert.pem files are reused when all of these hold:

  1. File existence — both files are present
  2. Expiry — the certificate is not expired or expiring within 5 minutes
  3. Not-before — the certificate is already valid
  4. SANs — they include localhost, 127.0.0.1, and ::1
  5. Key size — RSA keys are at least 2048-bit
  6. Key/cert pairing — the private key matches the certificate
  7. Self-signature — a self-issued certificate verifies against its own key

Certificates from your own CA (for example, mkcert) pass these checks and are kept. The Common Name is not checked.

If key.pem is readable by group or others, its mode is changed to 600 (Unix only). The files are kept.

If any check fails, the existing files are copied to key.pem.bak and cert.pem.bak (mode 600), then a new pair is generated. Older backups are never overwritten; later ones are named key.pem.1.bak, key.pem.2.bak, and so on. If the backup fails, nothing is 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
  • Logs are off by default — pass logger: console.log to see what happened

Windows: File permission checks are skipped on Windows.

Concurrency: A lock file (.generate-certs.lock) serializes concurrent calls. A lock is taken over only when its owner process is gone or it is older than 60 seconds.

Breaking Changes in v4 ⚠️

  • Requires Node.js >= 22.12.
  • activateLogs is replaced by logger. Logging is off by default. Pass logger: console.log to restore the v3 output.
  • The Common Name is no longer checked. Valid certificates with other CNs, or none, are kept instead of replaced.
  • CA-issued certificates are kept. v3 required a self-signature and replaced mkcert-style certificates.
  • An insecure key.pem mode is fixed, not replaced. The key keeps its content and gets mode 600. If the mode cannot be changed, generateCerts throws.
  • Invalid files are backed up before regeneration as *.bak.
  • Existing certificates are validated with node:crypto (X509Certificate), so EC and Ed25519 key pairs are accepted.
  • Generated certificates carry basicConstraints, keyUsage, and extKeyUsage.
  • node-forge is ^1.4.2, which fixes the October 2026 advisories (including PEM ReDoS and signature forgery).

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.

版本与发布记录

generate-certs 共发布 14 个版本,首次发布于 2024/11/20;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2026/11/04今天
3.0.02026/02/278 个月前
3.0.0-beta.0beta2026/02/158 个月前
2.1.12025/10/081.1 年前
2.1.02025/08/211.2 年前
2.0.22025/08/091.2 年前

关于 generate-certs 的常见问题

generate-certs 是什么?

️本地开发轻松生成HTTPS证书

如何安装 generate-certs?

在项目目录运行 npm install generate-certs,或使用 pnpm add / yarn add / bun add。

generate-certs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

generate-certs 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

generate-certs 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

generate-certs 可以商用吗?

generate-certs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

generate-certs 还在维护吗?

最新版本 4.0.0 发布于 2026/11/04(今天);至今共发布 14 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/11/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。