Convert HTML to Plain Text in Node.js
Practical Node.js methods for converting HTML to plain text locally, including parsing options, output checks, and common cleanup steps.
Before you start
Node.js does not include a built-in HTML-to-text converter, so you need a parsing approach. The process can run entirely on your machine, but first check whether the HTML is already available as a string or file. This matters because large files and remote pages need different handling.
For simple cases, you can write a small script that removes tags with a regular expression. This works without installing dependencies, but it is not a reliable parser for complex markup. If the input contains scripts, inline styles, encoded characters, or malformed tags, use a dedicated parsing package instead.
Make a copy of the source HTML before testing. Extraction should be reversible enough that you can compare the original file with the generated text if the result looks wrong.
Step-by-step instructions
Create a new Node.js script file and load the HTML. If the HTML is in a local file, use the file system module to read it as a string. If it comes from an API or another part of your application, pass that string directly to the conversion step.
Choose a conversion method. The simplest local approach is to remove markup by replacing tag patterns with a space and then decoding common character references. Keep this for controlled input where you know the markup is predictable.
For more dependable output, install a parser-based text extraction package. Add it only to the current project, then import it in your script. Most such libraries accept an HTML string and return plain text, but options for word wrapping, link handling, and heading formatting vary by version, so check the package documentation for the version you installed.
Write the extracted text to a new file or print it to the console. Avoid overwriting the original HTML file. If you are processing multiple files, keep the source filename in the output name so each result is easy to match.
Run the script from the terminal in the project directory. If the parser is not found, confirm that the dependency was installed locally rather than globally and that the script is using the correct import style for your Node.js version.
Check the output
Open the generated text file and look for the expected content from the source page. Check for leftover tags, repeated blank lines, and visible script or style text. These are signs that the conversion method did not handle part of the markup.
Compare a short section against the original HTML. Paragraph breaks, list items, and headings may not survive conversion unless the chosen method adds them. Plain text extraction usually flattens links into their visible labels, so do not expect URLs to remain unless that option is explicitly enabled.
Common problems and next steps
If the output is one long line, add line breaks after block-level elements before stripping tags, or use a parser that preserves block boundaries. If accented characters or symbols appear as codes, decode numeric and named character references after tag removal.
When scripts or styles leak into the result, remove those sections before extracting text rather than deleting every opening tag. The best correction depends on how the HTML was produced, so keep a sample of the original markup when adjusting the script.
For recurring conversions, wrap the working script into a small function that accepts an HTML string and returns plain text. That keeps the same cleaning rules in one place and makes it easier to reuse with files from a camera or recording workflow that include generated HTML notes.