htm
raw JSON →HTM (Hyperscript Tagged Markup) is a small JSX-like syntax for virtual DOM using standard JavaScript tagged templates, eliminating the need for a transpiler. Current stable version is 3.1.1, released with TypeScript module support improvements and package exports for Node 12+. It is actively maintained by the Preact team with a regular release cadence. HTM works in all modern browsers and is <600 bytes min+gzip. Key differentiators: no build step required, optional HTML-style quotes, component end-tags, HTML comments, and static subtree caching in v3. It provides off-the-shelf bindings for React and Preact, and can be customized with any hyperscript function. The package ships TypeScript type definitions, supports ESM and CJS, and can be compiled away with babel-plugin-htm for zero runtime cost in production.
Common errors
error TypeError: htm is not a function ↓
error Uncaught SyntaxError: The requested module 'https://unpkg.com/htm?module' does not provide an export named 'html' ↓
error Error: [htm] Could not find module 'preact' ↓
error Warning: Each child in a list should have a unique 'key' prop. ↓
Warnings
breaking In v3.0.0, static subtree caching was introduced, which may cause components with side effects in static children not to be re-rendered. ↓
breaking HTM v3 dropped support for CommonJS require() in favor of ESM-only exports. ↓
deprecated The htm/preact/standalone.module.js file is deprecated in favor of htm/preact/standalone (ESM) since v3.1.0. ↓
gotcha Spread operators for props must use triple dots: <div ...${props}>, not <div {...props}> which is JSX-only. ↓
breaking HTM v2 removed support for mixed static and dynamic property values that was later re-added in v2.2.0. If upgrading from v1, ensure property concatenation works as expected. ↓
Install
npm install htm yarn add htm pnpm add htm Imports
- default (htm function) wrong
const htm = require('htm')correctimport htm from 'htm' - html (Preact binding) wrong
import html from 'htm/preact'correctimport { html } from 'htm/preact' - html (React binding) wrong
import React from 'react'; import htm from 'htm'; const html = htm.bind(React.createElement)correctimport { html } from 'htm/react' - html + render (standalone) wrong
import { html, render } from 'htm/standalone'correctimport { html, render } from 'htm/preact/standalone' - Mini version wrong
import htm from 'htm/dist/mini'correctimport htm from 'htm/mini'
Quickstart
import { render } from 'https://unpkg.com/preact@latest?module';
import htm from 'https://unpkg.com/htm@latest?module';
const html = htm.bind(render);
const App = () => html`
<div class="app">
<h1>Hello, world!</h1>
<p>This is completely ${'dynamic'}.</p>
</div>
`;
render(html`<${App} />`, document.body);