A JavaScript parser
@babel/parser is the core parsing engine of the Babel toolchain, responsible for converting JavaScript source code into an Abstract Syntax Tree (AST). Originally forked from Acorn and formerly known as Babylon, it has evolved into the de facto standard for parsing modern JavaScript in build tools, linters, code transformers, and static analysis tools.
The parser handles ES2020+ syntax natively and uses a plugin architecture to support non-standard language extensions like JSX, TypeScript, Flow, and experimental TC39 proposals. Unlike simpler parsers, it produces rich AST nodes with configurable metadata including source locations, token streams, and range information, making it ideal for complex code transformation pipelines.
With over 117 million weekly downloads, @babel/parser powers countless developer tools including Babel itself, Prettier, webpack, ESLint plugins, and custom code analysis tools. It's designed for JavaScript developers building compilers, transpilers, bundlers, or any tooling that needs to understand JavaScript syntax structurally rather than just execute it.
The parser prioritizes extensibility and ecosystem compatibility over raw speed, making it the natural choice when working within the Babel ecosystem or when you need battle-tested parsing with extensive plugin support for emerging language features.
const parser = require('@babel/parser');
const fs = require('fs');
const sourceCode = `
import React from 'react';
export const Button = ({ label, onClick }) => {
return <button onClick={onClick}>{label}</button>;
};
`;
const ast = parser.parse(sourceCode, {
sourceType: 'module',
plugins: ['jsx'],
ranges: true,
tokens: true
});
console.log('Program type:', ast.program.sourceType);
console.log('Number of statements:', ast.program.body.length);
console.log('First import:', ast.program.body[0].type);
const exportDeclaration = ast.program.body.find(
node => node.type === 'ExportNamedDeclaration'
);
if (exportDeclaration) {
const variableDeclarator = exportDeclaration.declaration.declarations[0];
console.log('Exported component name:', variableDeclarator.id.name);
console.log('Is arrow function:', variableDeclarator.init.type === 'ArrowFunctionExpression');
}
console.log('\nTotal tokens parsed:', ast.tokens.length);
console.log('Total comments:', ast.comments.length);Building Babel plugins: Parse source code to traverse and transform specific AST nodes, such as converting modern syntax to ES5 or optimizing imports for tree-shaking.
Custom linting rules: Create project-specific code analysis tools that enforce architectural patterns, detect anti-patterns, or validate naming conventions by walking the AST.
Code documentation generators: Extract JSDoc comments, function signatures, and type annotations from source files to automatically generate API documentation.
Static analysis and metrics: Calculate code complexity, identify unused variables, map dependency graphs, or detect security vulnerabilities by analyzing the AST structure.
Source code transformation: Build codemods for large-scale refactoring, inject instrumentation for code coverage, or transpile custom DSLs embedded in JavaScript files.
npm install @babel/parserpnpm add @babel/parserbun add @babel/parser