Registry / serialization / common-tags

common-tags

JSON →
library1.0.3jsnpmunverified

common-tags provides a collection of reusable template literal tag functions for JavaScript (ES2015+). These tags offer functionalities like HTML escaping (`html`, `safeHtml`), string stripping and indentation (`stripIndent`, `oneLine`), and list formatting (`commaLists`, `inlineLists`). The current stable version is 1.8.2. While there's a pre-release v2.0.0-alpha.1 hinting at future TypeScript adoption and documentation rewrite, the package has a relatively stable, though not very frequent, release cadence for major features, with minor patches addressing bugs or internal tooling. It differentiates itself by offering a robust, well-tested set of common string manipulation utilities often needed when working with template literals, particularly for generating clean, formatted output like HTML or SQL, without the overhead of larger templating engines. It also provides an API for creating custom tags, emphasizing modularity and extensibility for custom text processing scenarios. Since version 1.8.0, it is entirely dependency-free, making it a lightweight choice for modern JavaScript projects.

npm install common-tags
INSTALL
IMPORT
SIG · COMMON-TAGS
C
common-tags
serializationjavascriptv1.0.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.

html
import { html } from 'common-tags';
const html = require('common-tags').html;
Primary way to import the `html` tag for safely processing HTML in template literals. CommonJS users should use named property access after require.
stripIndent
import { stripIndent } from 'common-tags';
import stripIndent from 'common-tags';
Used for removing leading indentation from multiline strings. All core tags are named exports; there is no default export for utility tags.
TemplateTag
import { TemplateTag } from 'common-tags';
import TemplateTag from 'common-tags/TemplateTag';
This class is used for creating custom template tags and is a named export from the main package. Direct import from a submodule path is incorrect.

This example demonstrates the use of `html` for basic templating with variable interpolation, `stripIndent` for cleaning up multiline strings, and `oneLine` for condensing strings to a single line, showcasing common use cases for the library's tags.

import { html, stripIndent, oneLine } from 'common-tags'; const user = { name: "Jane Doe", email: "jane.doe@example.com", id: "user-123", description: ` Jane is a passionate software engineer with a focus on front-end development. She loves open source and contributes to several projects. ` }; // Using 'html' tag for safe HTML generation and 'stripIndent' for formatting const userCard = html` <div id="${user.id}"> <h2>${user.name}</h2> <p>Email: <a href="mailto:${user.email}">${user.email}</a></p> <h3>About:</h3> ${stripIndent`${user.description}`} </div> `; // Using 'oneLine' tag for compact output const compactInfo = oneLine` User: ${user.name}, ID: ${user.id}, Email: ${user.email} `; console.log(userCard); console.log("\n--- Compact Info ---"); console.log(compactInfo);
Debug
Known issues
breakingIn `v2.0.0-alpha.1` (a pre-release), the `TemplateTag` class no longer automatically detects if an argument is a function and calls it. This change affects users who create custom tags and relied on this implicit behavior.
fix
Explicitly call functions passed as arguments within your custom `TemplateTag` implementation if you relied on the previous automatic function detection and execution.
affects: >=2.0.0-alpha.1
gotchaThe maintainer changed their GitHub handle from `declandewet` to `zspecza`. While fixed in `v1.8.1`, older references, documentation, or hardcoded repository links might be outdated or broken, potentially causing confusion or issues with tooling that relies on the old path.
fix
Ensure all references to the `common-tags` repository, especially in package managers or CI/CD configurations, point to `https://github.com/zspecza/common-tags`.
affects: <1.8.1
gotchaIn versions prior to `v1.8.0`, the order of calling `valueOf` and `toString` on objects in templates was non-spec compliant. This could lead to unexpected string coercion behavior compared to standard JavaScript template literals.
fix
Upgrade to `common-tags@1.8.0` or higher to ensure correct `valueOf` and `toString` call order, aligning with JavaScript specification.
affects: <1.8.0
Errors
Common errors & fixes
TypeError: common_tags_1.html is not a function
This typically occurs in TypeScript or bundled environments when `common-tags` (an ESM module or CJS with named exports) is imported incorrectly, often due to misconfiguration of `esModuleInterop` or incorrect default import usage.
fix
Ensure you are using named imports: `import { html } from 'common-tags';`. For CommonJS, use `const { html } = require('common-tags');`. Verify your TypeScript `tsconfig.json` includes `"esModuleInterop": true` if using mixed module types.
ReferenceError: html is not defined
The `html` tag function (or any other common-tags utility) was used without being correctly imported or destructured from the `common-tags` package.
fix
Add the necessary import statement at the top of your file: `import { html } from 'common-tags';` for ES Modules or `const { html } = require('common-tags');` for CommonJS.
TypeError: Template tag functions cannot be called directly
Template tags are special functions designed to be used with template literals (backticks). This error suggests you are attempting to call a tag like a regular function, e.g., `html('<div>Hello</div>')` instead of `html`Hello``.
fix
Always use template tag functions by preceding a template literal with the tag's name: `html`<div>Hello</div>`.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
common-tags — npm install common-tags · libregistry