solidity-parser

raw JSON →
0.4.0 verified Sat Apr 25 auth: no javascript deprecated

PEG.js-based Solidity parser for JavaScript. Current stable version is 0.4.0, released as pre-alpha software. Provides parse() and parseFile() functions to convert Solidity source code into a structured JSON AST, with an optional 'imports' mode to extract imported file paths. Intended for tooling that needs to analyze or transform Solidity code without relying on heavy preprocessors. The AST structure is unstable and incomplete, and the parser may silently skip unsupported Solidity constructs. Not recommended for production use.

error Error: Cannot find module 'pegjs'
cause Missing pegjs dependency when using package directly from GitHub or without proper install.
fix
Run 'npm install' in the project directory, or install pegjs globally with 'npm install -g pegjs'.
error SyntaxError: Unexpected token
cause Solidity code contains constructs not supported by the parser (e.g., newer Solidity syntax).
fix
Simplify or update the Solidity code to match older syntax; consider using a more modern parser like @solidity-parser/parser.
error TypeError: SolidityParser.parse is not a function
cause Using ES6 import style instead of CommonJS require.
fix
Use const SolidityParser = require('solidity-parser'); instead of import.
breaking The AST structure is unstable and changes between versions.
fix Do not rely on specific AST shapes; always test after upgrading.
deprecated This package is in pre-alpha and no longer actively maintained.
fix Consider using @solidity-parser/parser or other alternatives.
gotcha The parser silently ignores unsupported Solidity constructs.
fix Validate parsed output for completeness; use a more robust parser for production.
deprecated PEG.js dependency is outdated and may cause security vulnerabilities.
fix Upgrade to a parser built on a modern parser generator.
gotcha parseFile requires the file to exist and be readable; no error handling for missing files.
fix Ensure file exists before calling parseFile.
gotcha Output format may vary; documentation examples may not match actual output.
fix Inspect the actual returned JSON to understand the structure.
npm install solidity-parser
yarn add solidity-parser
pnpm add solidity-parser

Shows basic parse and parseFile usage with CommonJS require.

const SolidityParser = require('solidity-parser');

const solidityCode = `
contract Test {
  uint256 public value;
}
`;

const ast = SolidityParser.parse(solidityCode);
console.log(JSON.stringify(ast, null, 2));

// Extract imports from a file
const imports = SolidityParser.parseFile('./path/to/file.sol', 'imports');
console.log(imports);