HTML Tag Generator
The `html-tag` package provides a utility for generating HTML elements programmatically from JavaScript objects. It enables users to create HTML tags by specifying the tag name, an optional attributes object, and optional text content. The library correctly handles void (self-closing) elements and boolean attributes, simplifying basic HTML string construction. As of version 2.0.0, it offers a stable and lightweight solution for basic HTML generation, requiring Node.js 0.10.0 or higher. The package appears to be in maintenance mode, providing a focused API without frequent new feature releases. Its key differentiator is its straightforward, functional approach to HTML string creation, contrasting with more complex templating engines or JSX-based libraries, by focusing solely on direct string output for basic use cases. It does not provide advanced features like comprehensive HTML escaping (beyond basic attribute handling) or DOM manipulation, making it suitable for server-side HTML fragment generation.
Common errors
-
TypeError: tag is not a function
cause Attempting to import `tag` using incorrect ESM syntax (e.g., named import `import { tag } from 'html-tag';`) when the package provides a CommonJS default export.fixUse a default import for ESM (`import tag from 'html-tag';`) or the CommonJS `require` syntax (`const tag = require('html-tag');`). -
ReferenceError: require is not defined
cause Using the CommonJS `require` function within an ECMAScript Module (ESM) file context in Node.js without proper compatibility setup.fixChange your import statement to use `import tag from 'html-tag';` if your environment supports ESM. Alternatively, ensure your file is treated as a CommonJS module (e.g., by using a `.cjs` extension or setting `"type": "commonjs"` in `package.json`). -
SyntaxError: Unexpected token '{'cause This error can occur if you're trying to destructure a CommonJS default export in an environment that strictly parses `{}` as object destructuring and does not correctly handle CJS-ESM interop for default exports.fixUse a direct default import `import tag from 'html-tag';` instead of destructuring `import { tag } from 'html-tag';` for this CommonJS package.
Warnings
- gotcha The `html-tag` package does not automatically escape special characters within the `text` argument. Passing unvalidated user input directly as text content can lead to Cross-Site Scripting (XSS) vulnerabilities.
- gotcha Boolean attributes are handled strictly based on the boolean `true` or `false` values. Using a string like `'false'` will render the attribute with that string value (e.g., `open="false"`), not remove the attribute.
- gotcha The package primarily targets CommonJS environments. While modern Node.js may provide CJS-to-ESM interop, using `import tag from 'html-tag'` might behave unexpectedly or fail in some configurations, especially older ones or specific build tools.
- gotcha Passing `false` as the last argument to the `tag` function forces the tag to *not* render a closing tag (e.g., `tag('p', 'text', false)` results in `<p>text`). This behavior is intended for XML compatibility and can produce malformed HTML if not used carefully.
Install
-
npm install html-tag -
yarn add html-tag -
pnpm add html-tag
Imports
- tag
const tag = require('html-tag'); - tag (ESM default)
import { tag } from 'html-tag';import tag from 'html-tag';
- Type Definition
import type { TagFunction } from 'html-tag';
Quickstart
const tag = require('html-tag');
// Generate a simple anchor tag with text and attributes
console.log(tag('a', {href: 'https://example.com', target: '_blank'}, 'Visit Example'));
// Expected: <a href="https://example.com" target="_blank">Visit Example</a>
// Generate an image tag (a void element) with attributes
console.log(tag('img', {src: 'path/to/image.jpg', alt: 'Descriptive alt text'}));
// Expected: <img src="path/to/image.jpg" alt="Descriptive alt text">
// Generate a div with text content
console.log(tag('div', 'Hello, World!'));
// Expected: <div>Hello, World!</div>
// Generate a details tag with a boolean 'open' attribute
console.log(tag('details', {open: true}));
// Expected: <details open></details>
// Force a tag not to render its closing tag (for XML compatibility)
console.log(tag('p', 'Some text for XML...', false));
// Expected: <p>Some text for XML...