html-metadata-parser is a JavaScript library for Node.js environments, specializing in the extraction and parsing of metadata from HTML documents. It efficiently scrapes Open Graph (OG) tags, standard HTML meta tags, and image URLs from a given web page. The library provides a single, promise-based `parser` function that takes a URL as input and returns a structured object containing the discovered `og`, `meta`, and `images` data. The current stable version is 2.0.4, with development appearing to be active, although without a strictly defined public release cadence. Its key differentiator lies in its straightforward API for server-side metadata extraction, making it highly suitable for applications requiring functionalities like link previews, social media card generation, or general web content analysis. The package also ships with comprehensive TypeScript type definitions, providing an enhanced development experience for TypeScript users.
npm install html-metadata-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `html-metadata-parser` to asynchronously fetch and parse metadata from a given URL, including error handling.
Wrap calls to `parser` in a `try...catch` block and handle specific network error codes or messages. Consider retries with a backoff strategy for transient issues.
For very large documents, consider alternative parsing strategies or optimize resource handling. Monitor memory and CPU usage in production environments and apply timeouts to prevent hanging operations.
Implement delays between requests, cache results, or consider using a proxy rotation service if frequent requests to the same domain are necessary. Always respect `robots.txt`.
Always perform nullish coalescing or optional chaining (e.g., `result.og?.title`) when accessing properties of the parsed metadata object to prevent runtime errors.
Use optional chaining (`?.`) or nullish coalescing (`??`) when accessing metadata properties, e.g., `result.og?.title` or `result.meta?.description ?? 'No description'`. Always check if `result` itself is truthy.
Verify the URL is correct and accessible from your network. Check your DNS configuration or ensure there's no firewall blocking outbound requests. The error indicates the requested domain does not exist or cannot be reached.
Use named import for ESM: `import { parser } from 'html-metadata-parser';` or named require for CommonJS: `const { parser } = require('html-metadata-parser');`No dependency data recorded yet.