HTML5 Srcset Attribute Parser
parse-srcset is a JavaScript parser for the HTML5 `srcset` attribute, designed to be spec-conformant with the WHATWG reference algorithm. It takes a `srcset` string as input and returns an array of objects, each representing a potential image source with properties like `url`, `width`, `height`, and `density`. The package is at version 1.0.2, with its last known update around 10 years ago, indicating it is no longer actively maintained. While it provides a foundational implementation for `srcset` parsing, developers seeking current specifications, ongoing support, or additional features like stringification should consider more recently updated alternatives. Its release cadence was sporadic, reflecting its early development and subsequent abandonment, distinguishing it from modern, regularly updated libraries in this domain. The library relies on an extensive test suite to ensure conformance to the W3C srcset checker, a strong point at the time of its development.
Common errors
-
TypeError: parseSrcset is not a function
cause Attempting to use `import parseSrcset from 'parse-srcset';` when the package is a CommonJS module that exports the function directly (i.e., `module.exports = function(){...}`). ESM imports of CJS modules often wrap the default export under a `default` property, causing this error.fixChange the import statement to `const parseSrcset = require('parse-srcset');` in Node.js environments. If using a bundler, ensure it is configured to correctly handle CJS-ESM interoperability. For new projects in ESM, use `@prettier/parse-srcset` or `srcset`. -
ERR_REQUIRE_ESM
cause Trying to `require()` an ES Module or importing an old CJS module into a strict ESM context where `require` is not available or explicitly disallowed.fixThe `parse-srcset` package is CJS. Ensure your environment allows `require()` or use a bundler. For pure ESM projects, migrate to `@prettier/parse-srcset` or `srcset` which provide ESM compatibility. -
Failed parsing 'srcset' attribute value since it has an unknown descriptor.
cause The input `srcset` string contains syntax or descriptors (e.g., unsupported combinations of `w` and `x` descriptors) that the older parser version (1.0.2) does not recognize or support due to potential updates in the HTML specification or browser implementations since its last release.fixVerify the `srcset` string against the WHATWG HTML specification for `srcset`. If the syntax is valid according to current specs, the parser itself is outdated. Upgrade to a modern `srcset` parsing library like `@prettier/parse-srcset` or `srcset` which are actively maintained and more likely to be spec-compliant.
Warnings
- breaking This package (albell/parse-srcset) is largely unmaintained, with its last release over a decade ago (v1.0.2). It may not support newer srcset syntaxes or edge cases introduced after its last update.
- gotcha The `parse-srcset` package is a CommonJS module. Directly using `import parseSrcset from 'parse-srcset'` in an ES Module context without proper transpilation or bundler configuration will likely result in runtime errors (e.g., `TypeError: parseSrcset is not a function` or issues with default exports).
- deprecated The original `parse-srcset` library lacks modern tooling and features like TypeScript declarations. While functional, its maintenance status and lack of updates mean it will not receive bug fixes or performance improvements.
- gotcha Older parsers might not fully align with the most recent `srcset` specifications or common browser interpretations for complex or malformed inputs, potentially leading to 'Failed parsing srcset attribute' errors or incorrect output for specific descriptors like `w` and `x` in combination.
Install
-
npm install parse-srcset -
yarn add parse-srcset -
pnpm add parse-srcset
Imports
- parseSrcset
import parseSrcset from 'parse-srcset';
const parseSrcset = require('parse-srcset'); - parseSrcset
const parseSrcset = require('parse-srcset');import parseSrcset from '@prettier/parse-srcset';
- parse
import parse from 'parse-srcset';
import { parse } from 'srcset';
Quickstart
const parseSrcset = require('parse-srcset');
const srcsetString = 'elva-fairy-320w.jpg, elva-fairy-480w.jpg 1.5x, elva-fairy-640w.jpg 2x';
const parsed = parseSrcset(srcsetString);
console.log('Original srcset:', srcsetString);
console.log('Parsed output:');
parsed.forEach(item => {
console.log(`- URL: ${item.url}, Descriptor: ${item.density || (item.width ? item.width + 'w' : '')}`);
});
// Example with width descriptors (though this old version might interpret differently)
const widthSrcsetString = 'image-160.jpg 160w, image-320.jpg 320w, image-640.jpg 640w';
const parsedWidth = parseSrcset(widthSrcsetString);
console.log('\nOriginal srcset (width descriptors):', widthSrcsetString);
console.log('Parsed output (width descriptors):');
parsedWidth.forEach(item => {
console.log(`- URL: ${item.url}, Descriptor: ${item.width ? item.width + 'w' : item.density}`);
});