Utility Template Tags for ES2015+
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.
Common errors
-
TypeError: common_tags_1.html is not a function
cause 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.fixEnsure 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
cause The `html` tag function (or any other common-tags utility) was used without being correctly imported or destructured from the `common-tags` package.fixAdd 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
cause 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``.fixAlways use template tag functions by preceding a template literal with the tag's name: `html`<div>Hello</div>`.
Warnings
- breaking In `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.
- gotcha The 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.
- gotcha In 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.
Install
-
npm install common-tags -
yarn add common-tags -
pnpm add common-tags
Imports
- html
const html = require('common-tags').html;import { html } from 'common-tags'; - stripIndent
import stripIndent from 'common-tags';
import { stripIndent } from 'common-tags'; - TemplateTag
import TemplateTag from 'common-tags/TemplateTag';
import { TemplateTag } from 'common-tags';
Quickstart
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);