HTML Pretty Printer CLI
The `html` package, currently at version 1.0.0, functions as a command-line utility for pretty-printing HTML code. It is a direct Node.js port of `beautify-html.js`, which itself is based on `jsbeautifier`. The tool is designed primarily for CLI usage, accepting HTML input via `stdin`, file arguments, or interacting with the system clipboard through utilities like `pbpaste` and `pbcopy` on macOS. Its core purpose is to take minified or unformatted HTML and output a human-readable, indented version. Given its last update in 2012, the project is abandoned. Its release cadence is non-existent, and users are instructed to manually update its underlying `jsbeautifier` dependency, indicating a lack of active maintenance and potential compatibility issues with modern JavaScript environments or Node.js versions. There are no notable differentiators over modern HTML formatters beyond its extremely simple, direct CLI piping approach, which is now largely obsolete.
Common errors
-
html: command not found
cause The `html` CLI tool was not installed globally or is not in the system's PATH, or `npx` was not used.fixInstall the package globally: `npm install -g html` or run it via `npx html` to execute without global installation. -
TypeError: require is not a function
cause Attempting to use `require('html')` in an ES module context (`type: "module"` in `package.json` or `.mjs` file).fixIf programmatic use is desired, use a modern HTML formatter that supports ESM. If forced to use this package, you must use it in a CommonJS file or configure an ES module loader to interoperate with CJS modules. -
Error: Cannot find module 'html'
cause The package was not installed in the current project or is not correctly resolved by Node.js.fixRun `npm install html` in your project directory.
Warnings
- breaking This package is an abandoned CommonJS module from 2012 and is not compatible with modern ES module environments without specific loader configurations or transpilation. Attempting to `import` it will likely result in errors.
- deprecated The project is explicitly abandoned. The README instructs users to manually update its core `beautify-html.js` dependency by dropping new files into the `lib/` directory, rather than receiving updates via npm. This means the version of the underlying beautifier logic is extremely outdated and will not receive security patches or improvements.
- gotcha The package does not ship with TypeScript type definitions. Using it in a TypeScript project would require manually declaring types or ignoring type errors.
Install
-
npm install html -
yarn add html -
pnpm add html
Imports
- html CLI
import html from 'html'
npx html
- prettyPrint (CommonJS)
import { prettyPrint } from 'html';const { prettyPrint } = require('html');
Quickstart
import { exec } from 'node:child_process';
const uglyHtml = '<h2><strong><a href="http://awesome.com">AwesomeCom</a></strong><span>is awesome</span></h2>';
exec(`echo "${uglyHtml}" | npx html`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
console.log('Pretty Printed HTML:\n', stdout);
// Expected output will be formatted HTML like:
// <h2>
// <strong>
// <a href=http://awesome.com>AwesomeCom</a>
// </strong>
// <span>
// is awesome
// </span>
// </h2>
});