html-to-jsx

raw JSON →
0.0.4 verified Fri May 01 auth: no javascript maintenance

html-to-jsx is a lightweight, regex-based compiler that converts HTML strings into JSX syntax suitable for React applications. Version 0.0.4 is the latest stable release, though the package has seen no updates since its initial publication, indicating maintenance is stagnant. It differentiates from alternatives like html-react-parser by operating purely with regex without DOM parsing, making it fast but potentially brittle for complex or malformed HTML. The package is minimal, offering no TypeScript types, and is intended for simple conversion tasks where full HTML spec compliance is not required.

error Cannot find module 'html-to-jsx'
cause Package not installed or missing from dependencies.
fix
Run npm install html-to-jsx or add it to package.json.
error TypeError: htmlToJsx is not a function
cause Incorrect import of the module (e.g., using named import when default export is expected).
fix
Use default import: import htmlToJsx from 'html-to-jsx' or CommonJS: const htmlToJsx = require('html-to-jsx').htmlToJsx.
error htmlToJsx(...) is not a function
cause Import returned an object with a method named htmlToJsx instead of a function.
fix
Use const { htmlToJsx } = require('html-to-jsx') in CommonJS or import { htmlToJsx } from 'html-to-jsx' (if supported).
gotcha Regex-based parsing: does not handle nested or complex HTML structures correctly.
fix Use a proper HTML parser-based library (e.g., html-react-parser) for reliable conversion of complex HTML.
gotcha Self-closing tags conversion: may not convert all self-closing tags (e.g., <br>, <img>) correctly in all contexts.
fix Manually verify converted JSX for self-closing tags, or add trailing slashes in input HTML.
deprecated Package is unmaintained; no updates since 2015.
fix Consider migrating to actively maintained alternatives like html-react-parser or dangerouslySetInnerHTML.
gotcha Does not digest features like inline events, React Fragments, or JSX expressions.
fix Preprocess HTML to remove/replace unsupported features before conversion, or use a more comprehensive tool.
npm install html-to-jsx
yarn add html-to-jsx
pnpm add html-to-jsx

Converts a simple HTML string with class attribute to JSX className syntax.

import htmlToJsx from 'html-to-jsx';

const html = '<div class="container"><p>Hello World</p></div>';
const jsx = htmlToJsx(html);
console.log(jsx);
// Expected output: <div className='container'><p>Hello World</p></div>