{"id":18992,"library":"babel-file","title":"babel-file","description":"Utility to easily construct a Babel File object with code, AST, and path. v3.0.0 (latest) – stable, low maintenance. Provides a convenient wrapper around Babel core's File object, parsing code with Babel and returning the full file structure. Key differentiator: simplifies setup for Babel plugins/tools by handling File creation and error formatting. Requires Babel 7+ and babylon-options for parser options.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":null,"tags":["javascript"],"install":[{"cmd":"npm install babel-file","lang":"bash","label":"npm"},{"cmd":"yarn add babel-file","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-file","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used to create parser options in the example","package":"babylon-options","optional":true}],"imports":[{"note":"ESM-only; no CommonJS export","wrong":"const createFile = require('babel-file')","symbol":"default","correct":"import createFile from 'babel-file'"},{"note":"Only default export; named export is not available","wrong":"import { createFile } from 'babel-file'","symbol":"default","correct":"import createFile from 'babel-file'"},{"note":"Namespace import returns object with default property; use default import instead","wrong":"import * as babelFile from 'babel-file'","symbol":"default","correct":"import createFile from 'babel-file'"}],"quickstart":{"code":"import createFile from 'babel-file';\nimport createBabylonOptions from 'babylon-options';\n\nconst code = 'const x = 1;';\nconst parserOpts = createBabylonOptions({ stage: 2 });\nconst file = createFile(code, { filename: 'example.js', parserOpts });\n\nconsole.log(file.code);\nconsole.log(file.ast);\nconsole.log(file.path);","lang":"javascript","description":"Creates a Babel File object from source code, parsing with given options."},"warnings":[{"fix":"Upgrade Node.js and Babel to required versions.","message":"Version 3.0.0 dropped support for Node.js < 10 and Babel < 7.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Replace with inline parser options obj.","message":"babylon-options package is deprecated; use Babel's parser options directly.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Re-create File object after modifications.","message":"File object properties (code, ast, path) are not updated if you mutate the parsed code.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Validate parser options same as in Babel core.","message":"Missing or invalid parser options may cause crashes without informative error messages.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run npm install babel-file","cause":"Package not installed or not in node_modules","error":"Cannot find module 'babel-file'"},{"fix":"Use import createFile from 'babel-file'","cause":"Tried named import instead of default import","error":"createFile is not a function"},{"fix":"Ensure parserOpts matches Babel's parser options","cause":"Passed incorrect options format","error":"The 'parserOpts' option is not supported"},{"fix":"Check that code string is valid JavaScript","cause":"File creation failed due to invalid code","error":"Cannot read property 'code' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}