webidl2.js is the official W3C-maintained parser for Web IDL (Interface Definition Language), which is used to specify web APIs in an interoperable manner. The library provides core functionalities to `parse` Web IDL strings into a structured Abstract Syntax Tree (AST), `write` an AST back into a Web IDL string, and `validate` the semantic correctness of an AST. This enables programmatic analysis, modification, and generation of Web IDL definitions. It supports both Node.js (requiring Node.js 18 or higher) and browser environments, with distinct usage patterns for each. The current stable version is 24.5.0, reflecting a mature and specification-compliant implementation. Its key differentiators include direct W3C backing, comprehensive support for the Web IDL specification, and tools for both parsing and re-serializing IDL, making it an authoritative choice for developers working with Web IDL definitions.
npm install webidl2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a Web IDL string into an Abstract Syntax Tree (AST), re-serializing the AST back to a string, and validating its semantic correctness. It showcases the primary `parse`, `write`, and `validate` functions.
Upgrade your Node.js environment to version 18 or higher using `nvm install 18 && nvm use 18` or your preferred Node.js version manager.
For ESM files, use `import { parse, write, validate } from 'webidl2';`. For CommonJS files, use `const { parse, write, validate } = require('webidl2');`.If your AST processing logic depends on a specific structure, explicitly set the `concrete` option in `parse(idlString, { concrete: true/false })` to ensure consistent output.Refer to the `webidl2.js` documentation on 'Custom productions' and 'templates' for `write` to implement custom serialization logic.
Regularly run `npm update webidl2` and check the project's GitHub releases for important changes or security advisories.
Ensure you are using the correct import for your environment: `import { parse } from 'webidl2';` for Node.js ESM or `const { parse } = require('webidl2');` for Node.js CommonJS.Review the Web IDL string around the indicated line and column for syntax errors (e.g., missing semicolons, incorrect keywords, mismatched brackets). Use the online checker (w3c.github.io/webidl2.js/checker/) to validate the IDL.
Ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json` and `import` statements), or if you must use CommonJS, ensure your bundler/transpiler correctly handles the interop. For direct Node.js usage in CommonJS, sometimes an older version of the library might be needed if direct `require` is critical and ESM interop is not possible, but upgrading Node.js to a version that handles ESM better is usually the solution.
No dependency data recorded yet.