Koa Security Headers (Helmet Wrapper)
koa-helmet is a middleware collection for the Koa.js framework, providing essential HTTP security headers by wrapping the popular `helmet` library. It helps protect Koa applications from common web vulnerabilities by setting various headers like Content Security Policy (CSP), HSTS, X-Frame-Options, and more. The current stable version is 9.0.0, which notably introduces native ESM and CJS publishing without API changes. The package maintains an active release cadence, aligning with updates to both Koa (supporting v2 and v3) and Helmet (supporting versions 6, 7, and 8) via peer dependencies. A key differentiator is its minimal direct dependency footprint, relying solely on peer dependencies for `koa` and `helmet`, ensuring flexibility and control over core library versions in the consuming application. It ships with TypeScript typings, making it suitable for modern TypeScript-based Koa projects.
Common errors
-
Error: Cannot find module 'koa-helmet'
cause The `koa-helmet` package itself has not been installed, or there's a path resolution issue.fixRun `npm install koa-helmet` or `bun add koa-helmet`. If using CJS after v9, verify your import path for `require`. -
TypeError: Cannot read properties of undefined (reading 'use') at Object.<anonymous> (file.js:X:Y)
cause This typically indicates that `app` (your Koa instance) is not correctly initialized or the `koa` peer dependency is missing/misconfigured.fixEnsure you have `koa` installed (`npm install koa`) and that your `Koa` instance is correctly created as `const app = new Koa();`. -
Error: Cannot find module 'helmet' or 'koa'
cause koa-helmet relies on `helmet` and `koa` as peer dependencies, which must be installed separately.fixInstall the peer dependencies: `npm install helmet koa` or `bun add helmet koa`. -
TypeError: helmet is not a function
cause This usually happens when attempting to use a named export as a default export, or a CJS module attempting to import an ESM default incorrectly.fixIf using ESM, ensure `import helmet from 'koa-helmet';` is used for the default export. If trying to use a specific middleware, use named imports like `import { contentSecurityPolicy } from 'koa-helmet';`.
Warnings
- breaking Node.js version requirement increased. koa-helmet v7.0.1 dropped support for Node.js versions below 14. v9.0.0 requires Node.js >= 18.0.0.
- breaking Upgraded to Helmet v4.1.1 which introduced breaking changes in Helmet itself. This also involved dropping Node 8 support.
- breaking The package now publishes both ESM and CJS versions (dual package). While no API changes, consumers using older Node.js versions or specific bundler configurations might need to verify their import/require statements.
- gotcha koa-helmet has `helmet` and `koa` as peer dependencies. These must be explicitly installed alongside `koa-helmet` for the package to function correctly.
- gotcha In versions 8.0.0 through 8.0.2, the package inadvertently included `"type": "module"` in its package.json, which could lead to unexpected ESM treatment in some Node.js environments when intending to use CJS.
Install
-
npm install koa-helmet -
yarn add koa-helmet -
pnpm add koa-helmet
Imports
- helmet
const helmet = require('koa-helmet');import helmet from 'koa-helmet';
- contentSecurityPolicy
const { contentSecurityPolicy } = require('koa-helmet');import { contentSecurityPolicy } from 'koa-helmet'; - Koa
const Koa = require('koa');import Koa from 'koa';
Quickstart
import Koa from "koa";
import helmet from "koa-helmet";
const app = new Koa();
// Apply all default security headers provided by Helmet
app.use(helmet());
app.use((ctx) => {
ctx.body = "Hello World - Secured by Koa-Helmet!";
});
const PORT = process.env.PORT ?? 4000;
app.listen(PORT, () => {
console.log(`Koa app listening on http://localhost:${PORT}`);
});