HTML Validator CLI

raw JSON →
7.0.1 verified Thu Apr 23 auth: no javascript

html-validator-cli is a command-line interface tool for validating HTML documents against the W3C's validator.w3.org/nu service. It offers a straightforward way to check HTML files, URLs, or raw HTML strings directly from the terminal. The current stable version is 7.0.1. The project demonstrates a moderate release cadence, with several patch and minor updates, and significant major version bumps (6.0.0, 7.0.0) tied to underlying `html-validator` library updates and Node.js engine requirement changes. Key differentiators include support for various output formats (JSON, HTML, text), the ability to ignore specific error messages, and options to validate local files or use a custom validator endpoint. It simplifies HTML validation workflows for developers by integrating directly into shell scripts and CI/CD pipelines without needing a browser or GUI.

error Error: Command failed with exit code 1 Page not found
cause The validator could not access the provided URL, most commonly due to a local URL not being accessible from the W3C validator service or a typo in the URL.
fix
Ensure the URL is publicly accessible or, if validating a local development server, use the --islocal flag (e.g., html-validator http://localhost:8000 --islocal). Check the URL for typos.
error Error: 'html-validator' is not recognized as an internal or external command, operable program or batch file.
cause The `html-validator-cli` package was not installed globally or is not in the system's PATH, or `npx` is not being used.
fix
Install the package globally using npm i -g html-validator-cli or run it using npx html-validator ... to execute it without global installation.
error The 'engines' field in package.json specifies a Node.js version (>=10.22.0) that is not satisfied by your current environment.
cause Your installed Node.js version is older than the minimum required by html-validator-cli v7.x.
fix
Upgrade your Node.js version to 10.22.0 or newer. Alternatively, install an older compatible version of html-validator-cli (e.g., npm install -g html-validator-cli@6 for Node.js >= 8.15.3).
breaking Node.js engine requirement increased significantly. Version 7.x now requires Node.js >= 10.22.0. Previously, v6.x required Node.js >= 8.15.3, and v5.x supported older Node.js versions.
fix Upgrade your Node.js environment to version 10.22.0 or newer. If you must use an older Node.js, install an older version of html-validator-cli (e.g., `npm install html-validator-cli@6` for Node.js >= 8.15.3, or `npm install html-validator-cli@5` for older environments).
breaking Major version updates often coincide with significant changes in the underlying `html-validator` library (e.g., v6.x updated to `html-validator@4`, v7.x updated to `html-validator@5`). While the CLI aims to abstract these, subtle changes in error reporting, parsing, or supported features might occur.
fix Carefully review the release notes for both `html-validator-cli` and `html-validator` when upgrading major versions. Test your validation workflows thoroughly to ensure expected output and behavior.
gotcha The `--islocal` flag is crucial for validating local development URLs without exposing them publicly. Failing to use this flag for `http://localhost:...` or similar URLs will result in 'Page not found' errors as the W3C validator cannot access them.
fix Always use `html-validator <local-url> --islocal` when validating HTML served from a local development server.
gotcha By default, the CLI outputs 'Page is valid' or 'Page is not valid' to STDOUT and exits with code 0 or 1 respectively. Detailed error messages are suppressed unless `--verbose` is used or a specific `--format` is chosen.
fix To get detailed validation results, use `html-validator ... --verbose` or specify an output format like `html-validator ... --format=json`.
npm install html-validator-cli
yarn add html-validator-cli
pnpm add html-validator-cli

Demonstrates how to install html-validator-cli as a dev dependency and use it via npm scripts to validate a URL, a local file, or an HTML string.

{
  "name": "my-html-project",
  "version": "1.0.0",
  "description": "Validate HTML files in my project",
  "scripts": {
    "validate:url": "html-validator https://example.com --format=json",
    "validate:file": "html-validator --file=./public/index.html --verbose",
    "validate:data": "html-validator --data='<!DOCTYPE html><html><body><h1>Hello</h1></body></html>'"
  },
  "devDependencies": {
    "html-validator-cli": "^7.0.0"
  }
}

// To run from the command line after npm install:
// npm run validate:url
// npm run validate:file
// npm run validate:data