npm package
common-tags review
Use common-tags 1.8.2 when generated strings need several explicit formatting rules.
Use common-tags 1.8.2 when generated JavaScript strings need explicit whitespace rules. Its template tags remove indentation, join source lines and format array substitutions. Start with stripIndent when you want to remove a shared margin while keeping relative indentation. For multiline substitutions that need indentation, the README recommends html, also named source and codeBlock. Choose the tag according to the whitespace your output must retain.
Use common-tags 1.8.2 when generated strings need several explicit formatting rules. Start with stripIndent for shared-margin removal. Use codeBlock when multiline substitutions need indentation, then check your exact expected output in your application.
Answers from our run
common-tags or dedent: which should you use?
dedent: Consider dedent for an indentation-focused task: its 1.7.2 package description identifies it as a string tag that strips indentation from multiline strings. This guide does not test its output against common-tags. Use common-tags 1.8.2 when generated strings need several explicit formatting rules.
When should you not use common-tags?
Skip these transformations when output must preserve its original whitespace exactly.
Use it if
Skip it if
Setup reality
Install the exact version with npm install common-tags@1.8.2. The 1.8.2 package metadata lists lib as its main entry and es as its module entry. The matching README documents named imports and direct imports such as common-tags/lib/stripIndent. The examples below use CommonJS destructuring from the main entry. They are documentation-based illustrations: output comments describe expectations, not execution results. No installation, audit, benchmark or browser build was performed for this guide.
Runtime requirements need an application check. The package declares Node >=4.0.0, while the versioned README recommends Node 5 or higher and support for ES2015 tagged templates. It also mentions an Array.prototype.includes polyfill for older browsers. Those historical requirements do not verify compatibility with your current application. TypeScript definitions are available through @types/common-tags; the README says the TypeScript community maintains them separately from the package authors.
Choose stripIndent to retain relative indentation, or stripIndents to remove indentation from every line. Both trim surrounding whitespace, so neither suits output that must retain its original boundaries. stripIndents can flatten nested code or fixtures. When an inserted string contains newlines, stripIndent does not reindent its later lines. The README recommends html, source or codeBlock for that case.
Choose oneLine when source line breaks should become spaces. Choose oneLineTrim when those breaks should disappear without added spaces; adjacent words can then merge. The URL example below uses trusted static pieces. Dynamic query values still need encoding. Array tags produce display lists rather than serialization formats, and a comma-separated display list is not a CSV encoder.
Escaping remains outside this guide's verified scope. The supplied README extraction does not reliably preserve escaped output, so this guide omits a safeHtml output example and character-replacement table. No implementation verification or security testing was performed. This extraction limitation is not a demonstrated defect in the library or its documentation. Keep the generated fragments in these examples trusted and assess security-sensitive rendering separately.
Release evidence supports a narrow maintenance conclusion. The 1.8.2 release notes describe a publishing-build fix using the same code as 1.8.1. They do not establish present support or maintainer responsiveness. Before adopting a tag, write an assertion for your application's exact expected string, including blank lines and multiline substitutions.
Reader-visible ratings are computed using the site's published rubric. The dimension notes explain the inspected evidence: documented API behavior and release continuity for API stability, task examples and requirements for documentation, the release repair for maintenance, and documented integration paths for ecosystem. Runtime compatibility, current adoption and maintainer response times remain unmeasured and are not credited as demonstrated strengths. The README extraction limitation is not counted as a documentation defect.
Patterns
Keep relative indentation in CLI help help-text
const { stripIndent } = require('common-tags');
const help = stripIndent`
Usage: report <file>
--json Write JSON output
`;
console.log(help);
// Expected, not executed:
// Usage: report <file>
// --json Write JSON outputThe shared margin disappears while the option line retains its relative indentation. Surrounding whitespace is trimmed.
Remove indentation from every line flush-left-text
const { stripIndents } = require('common-tags');
const notice = stripIndents`
Upload complete.
Check your inbox for the link.
`;
console.log(notice);
// Expected, not executed:
// Upload complete.
// Check your inbox for the link.The second line loses its deeper indentation. Avoid this transformation for nested code or fixtures that depend on indentation.
Spread a sentence across source lines single-line-message
const { oneLine } = require('common-tags');
const message = oneLine`
Your export is ready.
Download it from the reports page.
`;
console.log(message);
// Expected, not executed:
// Your export is ready. Download it from the reports page.Use this when the output should contain a space between the source lines. It is not a general-purpose minifier.
Join trusted static URL pieces join-url-pieces
const { oneLineTrim } = require('common-tags');
const url = oneLineTrim`
https://example.com/reports
?format=json
`;
console.log(url);
// Expected, not executed:
// https://example.com/reports?format=jsonJoining without a separator suits these static pieces. Encode dynamic query values separately; the tag does not establish URL validity.
Insert an array as a display list inline-array
const { inlineLists } = require('common-tags');
const formats = ['json', 'csv'];
const message = inlineLists`Available formats: ${formats}`;
console.log(message);
// Expected, not executed:
// Available formats: json csvThis example uses an inline substitution. Check indentation separately if you move the array into an indented multiline template.
Display comma-separated options comma-separated-options
const { commaLists } = require('common-tags');
const formats = ['json', 'csv', 'txt'];
const message = commaLists`Formats: ${formats}`;
console.log(message);
// Expected, not executed:
// Formats: json, csv, txtThis produces display text. If another program must parse the result, use an encoder for that format rather than treating it as CSV.
End a display list with and conjunction-list
const { commaListsAnd } = require('common-tags');
const message = commaListsAnd`Includes ${['logs', 'traces', 'metrics']}.`;
console.log(message);
// Expected, not executed:
// Includes logs, traces and metrics.The documented conjunction is English, and this example has no serial comma. It does not establish locale-aware list formatting.
Indent an embedded source block multiline-source-substitution
const { codeBlock } = require('common-tags');
const body = 'start();\nfinish();';
const generated = codeBlock`
function run() {
${body}
}
`;
console.log(generated);
// Expected, not executed:
// function run() {
// start();
// finish();
// }The README identifies codeBlock as an alias of html and recommends it for multiline substitutions. Formatting a trusted fragment does not validate the generated code.
Remove indentation from an existing string clean-existing-string
const { stripIndent } = require('common-tags');
const fixture = ' parent\n child';
const cleaned = stripIndent(fixture);
console.log(JSON.stringify(cleaned));
// Expected, not executed:
// "parent\n child"The README documents calling a tag directly with a normal string. Keep the original separately when its whitespace matters.
Alternatives
| Package | Registry | Pick it when |
|---|---|---|
| dedent | npm | Consider dedent for an indentation-focused task: its 1.7.2 package description identifies it as a string tag that strips indentation from multiline strings. This guide does not test its output against common-tags. |
| strip-indent | npm | Consider strip-indent when your task starts with a string: its 4.1.1 package description says it strips leading whitespace from each line. This guide does not establish identical trimming behavior or interchangeability. |
More web backend guides
How this guide is made: grounded in the library's documentation, release notes, changelog, and issue history, on a fixed rubric — not a hands-on install of every release. The 50 most-downloaded entries are additionally install-verified in clean containers. Corrections: contact the desk.