babel-file-loader

raw JSON →
2.0.0 verified Sat Apr 25 auth: no javascript

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.

error Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
cause Using require('babel-file-loader') in a CommonJS context.
fix
Use dynamic import: const { loadImportSync } = await import('babel-file-loader'); or switch your project to ES modules.
error SyntaxError: Unexpected token 'export'
cause Using ES module syntax in a .js file without type:module or .mjs extension.
fix
Rename the file to .mjs or add "type": "module" to your package.json.
error TypeError: path.traverse is not a function
cause Attempting to traverse the path object directly instead of the File object's path property returned by loadImportSync.
fix
Use file.path.traverse({...}) where file is the return value of loadImportSync.
error Error: Cannot find module 'babylon'
cause Missing peer dependency for Babylon parser.
fix
Install babylon: npm install babylon@latest
breaking 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.
fix Upgrade to v2.0.0 and adapt code to use new sync/async naming (e.g., loadImportSync instead of callback-based loadImport).
gotcha The package is ESM-only (type: module). Using require() or CommonJS will fail with ERR_REQUIRE_ESM.
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.
gotcha loadImportSync may throw if the import path cannot be resolved or parsed; always wrap in try/catch inside visitor functions.
fix Wrap calls in try/catch blocks to prevent Babel plugin from crashing.
deprecated The underlying Babylon parser is deprecated in favor of @babel/parser. This package still uses Babylon; may break with newer Babel versions.
fix Consider using @babel/traverse and @babel/core's programmatic API for multi-file analysis.
gotcha 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.
fix Prefer sync variants (loadImportSync) for use inside Babel visitors; async functions may not work as expected.
npm install babel-file-loader
yarn add babel-file-loader
pnpm add babel-file-loader

Shows how to use loadImportSync inside a Babel plugin visitor to load and parse the file referenced by an import declaration.

import { loadImportSync } from 'babel-file-loader';

export default function() {
  return {
    visitor: {
      ImportDeclaration(path) {
        try {
          const file = loadImportSync(path);
          console.log('Loaded file:', file.opts.filename);
        } catch (e) {
          console.error('Failed to load import:', e.message);
        }
      }
    }
  };
}