Registry / serialization / string-strip-html

string-strip-html

JSON →
library13.5.3jsnpmunverified

The `string-strip-html` package, currently at version 13.5.3, is a robust utility designed to efficiently remove HTML tags from arbitrary string inputs. It is actively maintained as part of the Codsen monorepo, with updates released as needed for feature enhancements and bug fixes. A key differentiator is its "no parser" approach, meaning it does not attempt to fully parse or validate HTML. This design makes it exceptionally resilient and suitable for stripping tags from malformed, incomplete, or "mixed" HTML sources often found in user-generated content or messy data, where traditional DOM parsers might fail or be overkill. It provides granular control over which tags to strip and offers options to remove entire tag-content blocks (e.g., `<script>...</script>`). Since version 9.0.0, the library is distributed exclusively as an ECMAScript Module (ESM), aligning with modern JavaScript ecosystem standards and requiring `import` syntax.

npm install string-strip-html
INSTALL
IMPORT
SIG · STRING-STRIP-HTML
S
string-strip-html
serializationjavascriptv13.5.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stripHtml
import { stripHtml } from 'string-strip-html';
const { stripHtml } = require('string-strip-html');
The package is pure ESM since v9. For CommonJS environments, use version 8.5.0 or earlier.
stripHtml
import { stripHtml } from 'string-strip-html';
import stripHtml from 'string-strip-html';
`stripHtml` is a named export, not a default export.
StripHtmlOptions
import type { StripHtmlOptions } from 'string-strip-html';
TypeScript type import for configuring `stripHtml` behavior.

Demonstrates basic HTML stripping, whitespace handling, stripping tag contents, and proper handling of angle brackets that are not HTML tags.

import { strict as assert } from "assert"; import { stripHtml } from "string-strip-html"; // Basic usage: removes HTML tags, replacing them with a single space. assert.equal( stripHtml("Some text <b>and</b> text.").result, "Some text and text." ); // Handles consecutive tags gracefully by collapsing whitespace. assert.equal(stripHtml("aaa<div>bbb</div>ccc").result, "aaa bbb ccc"); // Option to strip specific tags along with their entire contents. assert.equal( stripHtml("a <pre><code>void a;</code></pre> b", { stripTogetherWithTheirContents: [ "script", // default "style", // default "xml", // default "pre" // custom-added ] }).result, "a b" ); // Intelligently distinguishes between HTML tags and legitimate angle brackets. assert.equal(stripHtml("a < b and c > d").result, "a < b and c > d");
Debug
Known issues
breakingThe package was converted to pure ECMAScript Modules (ESM) starting from version 9.0.0. CommonJS `require()` statements will no longer work.
fix
Update your project to use ES modules with `import` syntax, or explicitly install version 8.5.0 (`npm i string-strip-html@8.5.0`) for CommonJS compatibility.
affects: >=9.0.0
gotchaThis library does not parse HTML like a DOM parser. It works by detecting patterns that resemble HTML tags. It will not correct malformed HTML, balance tags, or provide a DOM tree.
fix
If you require a full HTML parser for validation, manipulation, or complex error correction, consider libraries like `jsdom` or `cheerio` instead. This library is best for simple, robust tag removal.
affects: >=1.0.0
gotchaThe `stripTogetherWithTheirContents` option completely removes specified tags and everything between their opening and closing tags. Be mindful when adding common HTML structural tags like `div` or `span`.
fix
Carefully select which tags to include in `stripTogetherWithTheirContents`. Use this option primarily for tags whose content is irrelevant after stripping (e.g., `<script>`, `<style>`, `<pre>`) rather than general layout tags.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: require is not a function
Attempting to `require()` `string-strip-html` in a CommonJS context when the installed version is ESM-only.
fix
If using Node.js, ensure your package is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { stripHtml } from 'string-strip-html';`. Alternatively, downgrade to version 8.5.0 for CommonJS support: `npm install string-strip-html@8.5.0`.
SyntaxError: Named export 'stripHtml' not found. The requested module 'string-strip-html' does not provide an export named 'stripHtml'
Attempting to use a default import (`import stripHtml from 'string-strip-html';`) when `stripHtml` is a named export.
fix
Change the import statement to use named import syntax: `import { stripHtml } from 'string-strip-html';`.
Upgrade
Version history
13.5.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
string-strip-html — npm install string-strip-html · libregistry