{"id":10554,"library":"babylon","title":"Babel Parser (formerly Babylon)","description":"Babel Parser, previously known as Babylon, is a highly configurable JavaScript parser that generates an Abstract Syntax Tree (AST) from source code. It is a fundamental component of the Babel toolchain, used for transpiling modern JavaScript into compatible versions. Currently stable at version 7.x, it receives frequent updates, including beta releases for upcoming ECMAScript features. Its key differentiators include a pluggable architecture supporting a wide array of experimental and standard JavaScript syntax extensions like JSX, Flow, and TypeScript, as well as new proposals like nullish coalescing, optional chaining, and the pipeline operator. The parser offers both `parse` for full programs and `parseExpression` for single expressions, along with options for comment attachment and error recovery, providing detailed AST output based on the ESTree spec with Babel's specific deviations.","status":"renamed","version":"6.18.0","language":"javascript","source_language":"en","source_url":"https://github.com/babel/babylon","tags":["javascript","babel","parser","babylon"],"install":[{"cmd":"npm install babylon","lang":"bash","label":"npm"},{"cmd":"yarn add babylon","lang":"bash","label":"yarn"},{"cmd":"pnpm add babylon","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Commonly used in conjunction with @babel/parser for traversing and manipulating the generated AST.","package":"@babel/traverse","optional":true}],"imports":[{"note":"The `babylon` package is deprecated. For Babel 7+, use `@babel/parser` which is ESM-first, though CommonJS `require` is also supported for backwards compatibility.","wrong":"const parse = require('babylon').parse;","symbol":"parse","correct":"import { parse } from '@babel/parser';"},{"note":"`parseExpression` is used for parsing single JavaScript expressions efficiently, whereas `parse` is for full programs. Follows the same package migration as `parse`.","wrong":"import { parseExpression } from 'babylon';","symbol":"parseExpression","correct":"import { parseExpression } from '@babel/parser';"},{"note":"Import types using `import type` for better type safety and to ensure they are stripped from the compiled output.","wrong":"import { ParserOptions } from '@babel/parser';","symbol":"ParserOptions","correct":"import type { ParserOptions } from '@babel/parser';"}],"quickstart":{"code":"import { parse } from '@babel/parser';\n\nconst code = `\n  function greet(name: string): string {\n    const message = `Hello, ${name}!!`;\n    return <p>{message}</p>;\n  }\n\n  const result = greet('World');\n  console.log(result);\n`;\n\ntry {\n  const ast = parse(code, {\n    sourceType: 'module',\n    plugins: ['typescript', 'jsx']\n  });\n  console.log('Successfully parsed AST:');\n  // A simplified log to show the top-level structure, \n  // the full AST can be very large.\n  console.log(JSON.stringify(ast.program.body[0], null, 2));\n  console.log('...');\n\n  // Example of accessing a node\n  const functionDeclaration = ast.program.body[0];\n  if (functionDeclaration.type === 'FunctionDeclaration') {\n    console.log(`Function name: ${functionDeclaration.id.name}`);\n  }\n\n} catch (error) {\n  console.error('Parsing failed:', error.message);\n}\n","lang":"typescript","description":"Demonstrates parsing a TypeScript and JSX mixed-syntax code snippet into an AST, enabling the necessary plugins for correct parsing."},"warnings":[{"fix":"Replace `babylon` with `@babel/parser` in your `package.json` and update import/require statements accordingly.","message":"The `babylon` npm package was officially renamed to `@babel/parser` with the release of Babel 7. Projects should migrate to the `@babel/parser` package to receive updates and align with the Babel ecosystem. The `babylon` package is no longer maintained.","severity":"breaking","affected_versions":">=6.18.0 (for migration to v7)"},{"fix":"Upgrade your Node.js runtime to a version officially supported by Babel 7+ (e.g., Node.js 14+ or newer LTS releases).","message":"Babel 7, which includes `@babel/parser`, dropped support for older Node.js versions, specifically 0.10, 0.12, 4, and 5. Ensure your environment uses a supported Node.js version (typically Node.js 6+ at the time of Babel 7's release, now much higher).","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Review the Babel 7 migration guide and update any AST traversal or manipulation logic to account for the new node names and structures. Use AST Explorer to visualize differences.","message":"The AST format has undergone several breaking changes between Babylon 6 and `@babel/parser` 7. Notable changes include `PrivateName.name` being renamed to `.id` (v7.0.0-beta.22) and clearer separation of Flow and TypeScript specific AST node types (v7.0.0-beta.25).","severity":"breaking","affected_versions":">=7.0.0-beta.22"},{"fix":"Always pass a `plugins` array to the `parse` or `parseExpression` options, e.g., `{ plugins: ['jsx', 'typescript'] }`, to enable desired syntaxes.","message":"By default, `@babel/parser` only supports standard ECMAScript syntax. Parsing non-standard features like JSX, Flow, TypeScript, or experimental language proposals requires explicitly enabling the corresponding plugins in the parser options. Failing to do so will result in a `SyntaxError: Unexpected token`.","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"Set `sourceType: 'module'` for files intended as ES Modules. Consider `sourceType: 'unambiguous'` if the module format is unknown, which attempts to guess based on import/export statements.","message":"The default `sourceType` for parsing is 'script'. For code containing ES Modules (`import`/`export` statements), you must set `sourceType: 'module'` in the parser options. Incorrect `sourceType` can lead to parsing errors or unexpected behavior.","severity":"gotcha","affected_versions":">=6.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Enable the appropriate plugin(s) in the `plugins` array within the parser options (e.g., `plugins: ['jsx', 'typescript']`) or review your code for genuine syntax errors. Check the `sourceType` option if dealing with modules.","cause":"The parser encountered syntax it does not recognize, usually because a required plugin for a non-standard feature (e.g., JSX, TypeScript) was not enabled in the options, or there is an actual syntax error in the code.","error":"SyntaxError: Unexpected token"},{"fix":"If using `@babel/parser` (v7+), prefer ES Module `import` syntax: `import { parse } from '@babel/parser';`. Ensure your environment supports ES Modules (e.g., Node.js 14+ or configure Babel/Webpack for transpilation). If targeting older Node, consider using a CommonJS wrapper or transpiling your parsing script.","cause":"This error typically occurs when attempting to use CommonJS `require()` in an ES Module environment, or when running `@babel/parser` code (which is primarily ESM) directly in an environment that doesn't support ES Modules (like older Node.js versions without transpilation or specific flags) or expects a CommonJS export from the deprecated `babylon` package.","error":"ReferenceError: require is not defined"},{"fix":"Ensure `@babel/preset-env` (and any other necessary presets) are installed and correctly configured in your project's Babel configuration files (e.g., `babel.config.js`, `.babelrc`). If it's an ESLint-specific issue in an IDE, adjust ESLint working directories or ensure ESLint's parser is correctly configured, typically to `@babel/eslint-parser` which then uses `@babel/parser` internally.","cause":"This error often arises in build tools like ESLint when they attempt to use `@babel/parser` via a configuration that expects Babel presets to be available during parsing, but they are either missing or the environment is misconfigured (e.g., monorepo setup, wrong working directory).","error":"Parsing error: Cannot find module '@babel/preset-env'"}],"ecosystem":"npm"}