Helmet is a popular middleware package for Express and Connect applications, designed to enhance web security by automatically setting various HTTP response headers. The current stable version is 8.1.0, compatible with Node.js 18 and later. Helmet typically releases major versions at a moderate pace, incorporating updates to security best practices and deprecating outdated headers. Its key differentiator is its ease of use, providing a sensible default set of 12 security headers out-of-the-box, including `Content-Security-Policy`, `Cross-Origin-Opener-Policy`, and `Strict-Transport-Security`. While providing robust defaults, Helmet is highly configurable, allowing developers to fine-tune individual header directives or disable specific headers entirely to suit their application's needs, making it a go-to solution for foundational web security in Node.js environments.
npm install helmetVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize Helmet with default security headers in an Express application and provides an example of custom Content Security Policy (CSP) configuration.
Migrate your project to use ESM `import` statements and configure `package.json` with `"type": "module"` if necessary. If a CJS-only project, ensure appropriate loaders or bundler configurations are in place to handle ESM dependencies.
Upgrade your Node.js environment to version 18 or higher.
Review your application's security requirements and explicitly enable any previously relied-upon headers if still needed, or use separate, dedicated packages for removed functionalities (e.g., `expect-ct` package for `Expect-CT`). Note that `X-XSS-Protection` is often disabled for security reasons.
Thoroughly test your CSP in a development environment. Start with `reportOnly: true` to log violations without enforcing them. Gradually refine directives, explicitly allowing necessary sources and considering `nonce` attributes or hashes for inline scripts/styles instead of `unsafe-inline` where possible.
Place `app.use(helmet());` at the beginning of your middleware stack, before defining routes or other middleware that might send responses.
Change your import statement from `const helmet = require('helmet');` to `import helmet from 'helmet';`. Ensure your `package.json` specifies `"type": "module"` if it's a pure ESM project, or use a bundler that handles ESM correctly.Modify your `contentSecurityPolicy` directives in Helmet to include the allowed domain for scripts, e.g., `scriptSrc: ["'self'", 'https://cdn.example.com']`. For inline scripts, consider using `nonce` attributes or hashes.
For ESM, ensure you use `import helmet from 'helmet';`. For CommonJS in older projects, use `const helmet = require('helmet');` and then `app.use(helmet());`.No dependency data recorded yet.