{"id":18991,"library":"babel-file-loader","title":"babel-file-loader","description":"Load files into memory for parsing and traversal with Babylon/Babel. Version 2.0.0 is the latest stable release, published as part of the babel-multi-file project. It provides async and sync methods to resolve file paths from import declarations and load parsed File objects, enabling multi-file Babel plugin analysis. Key differentiators: integrates directly with Babel's visitor pattern and File representation, offering low-level control over file resolution and parsing.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/thejameskyle/babel-multi-file","tags":["javascript"],"install":[{"cmd":"npm install babel-file-loader","lang":"bash","label":"npm"},{"cmd":"yarn add babel-file-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-file-loader","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package ships ES modules; named exports only, no default export. Use ESM import syntax.","wrong":"const loadImportSync = require('babel-file-loader').loadImportSync","symbol":"loadImportSync","correct":"import { loadImportSync } from 'babel-file-loader'"},{"note":"Default import is incorrect; all exports are named.","wrong":"import loadFileSync from 'babel-file-loader'","symbol":"loadFileSync","correct":"import { loadFileSync } from 'babel-file-loader'"},{"note":"CommonJS require is not supported in the ESM distribution; use dynamic import or ES module context.","wrong":"const { resolveFilePathSync } = require('babel-file-loader')","symbol":"resolveFilePathSync","correct":"import { resolveFilePathSync } from 'babel-file-loader'"}],"quickstart":{"code":"import { loadImportSync } from 'babel-file-loader';\n\nexport default function() {\n  return {\n    visitor: {\n      ImportDeclaration(path) {\n        try {\n          const file = loadImportSync(path);\n          console.log('Loaded file:', file.opts.filename);\n        } catch (e) {\n          console.error('Failed to load import:', e.message);\n        }\n      }\n    }\n  };\n}","lang":"javascript","description":"Shows how to use loadImportSync inside a Babel plugin visitor to load and parse the file referenced by an import declaration."},"warnings":[{"fix":"Upgrade to v2.0.0 and adapt code to use new sync/async naming (e.g., loadImportSync instead of callback-based loadImport).","message":"Version 2.0.0 is a complete rewrite from the previous babel-file-loader package (v1). API changed entirely: sync methods now return objects instead of using callbacks, and all functions are async/sync variants.","severity":"breaking","affected_versions":"~1.x"},{"fix":"Use dynamic import() inside CommonJS, or switch to ES module context. For Babel plugins, ensure your plugin file uses .mjs or \"type\": \"module\" in package.json.","message":"The package is ESM-only (type: module). Using require() or CommonJS will fail with ERR_REQUIRE_ESM.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Wrap calls in try/catch blocks to prevent Babel plugin from crashing.","message":"loadImportSync may throw if the import path cannot be resolved or parsed; always wrap in try/catch inside visitor functions.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Consider using @babel/traverse and @babel/core's programmatic API for multi-file analysis.","message":"The underlying Babylon parser is deprecated in favor of @babel/parser. This package still uses Babylon; may break with newer Babel versions.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Prefer sync variants (loadImportSync) for use inside Babel visitors; async functions may not work as expected.","message":"All async functions (e.g., loadImport, loadFile) are not exported as promises; they use callback-style? The docs mention Async/Sync variants but actual async behavior is unclear.","severity":"gotcha","affected_versions":"2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use dynamic import: const { loadImportSync } = await import('babel-file-loader'); or switch your project to ES modules.","cause":"Using require('babel-file-loader') in a CommonJS context.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module not supported"},{"fix":"Rename the file to .mjs or add \"type\": \"module\" to your package.json.","cause":"Using ES module syntax in a .js file without type:module or .mjs extension.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Use file.path.traverse({...}) where file is the return value of loadImportSync.","cause":"Attempting to traverse the path object directly instead of the File object's path property returned by loadImportSync.","error":"TypeError: path.traverse is not a function"},{"fix":"Install babylon: npm install babylon@latest","cause":"Missing peer dependency for Babylon parser.","error":"Error: Cannot find module 'babylon'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}