find-babel-config
raw JSON → 2.1.2 verified Sat Apr 25 auth: no javascript
A lightweight utility to locate the closest Babel configuration file (babel.config.js, babel.config.json, .babelrc, etc.) from a given directory, searching upwards. Version 2.1.2 is stable and actively maintained. It supports both async and sync APIs with config depth control. Compared to alternatives like cosmiconfig, it specializes in Babel configs and has no direct dependency on Babel itself, making it a simple drop-in for Babel-integrated tools. The package is ESM- and CJS-compatible.
Common errors
error Cannot find module 'find-babel-config' ↓
cause Package not installed or not in node_modules.
fix
Run 'npm install find-babel-config' or add to package.json.
error findBabelConfig.sync is not a function ↓
cause CommonJS require returns the async function; sync is not automatically imported.
fix
Use 'const findBabelConfig = require('find-babel-config'); const sync = findBabelConfig.sync;'
error TypeError: findBabelConfig is not a function ↓
cause Default import from ESM might fail if bundler treats it incorrectly.
fix
Ensure bundler supports CommonJS interop. Use 'import pkg from 'find-babel-config'; const findBabelConfig = pkg.default || pkg;'
error ENOENT: no such file or directory, scandir '/some/path' ↓
cause The provided directory does not exist.
fix
Verify the directory path exists before calling findBabelConfig.
Warnings
breaking In version 2.0.0, the API changed from callback-based to promise-based async. ↓
fix Use promises or async/await instead of callbacks.
gotcha The sync method may throw errors if the directory does not exist, unlike async which rejects the promise. ↓
fix Wrap sync calls in try-catch.
gotcha Depth parameter is inclusive: setting depth=1 means only the given directory is searched, no parent. ↓
fix Use depth=Infinity (default) for unlimited search, or depth=n to search n levels up.
deprecated The package does not support Babel configuration as an ES module (babel.config.mjs) or TypeScript (babel.config.ts). ↓
fix Use cosmiconfig for broader config file support.
gotcha When using relative directory paths, they are resolved from process.cwd(), not from the module's location. ↓
fix Pass absolute paths to avoid ambiguity.
Install
npm install find-babel-config yarn add find-babel-config pnpm add find-babel-config Imports
- findBabelConfig wrong
const findBabelConfig = require('find-babel-config')correctimport findBabelConfig from 'find-babel-config' - findBabelConfigSync wrong
import { sync } from 'find-babel-config'correctimport findBabelConfig from 'find-babel-config'; const sync = findBabelConfig.sync - Result wrong
import { Result } from 'find-babel-config'correctimport findBabelConfig from 'find-babel-config'; type Result = { file: string | null; config: any }
Quickstart
import findBabelConfig from 'find-babel-config';
// Async usage
const directory = process.cwd();
findBabelConfig(directory)
.then(({ file, config }) => {
if (file) {
console.log('Found config at:', file);
console.log('Config:', JSON.stringify(config, null, 2));
} else {
console.log('No babel config found');
}
})
.catch(err => {
console.error('Error finding config:', err);
});
// Sync usage
const { file, config } = findBabelConfig.sync(directory);
if (file) {
console.log('Sync found config at:', file);
}