Babel plugin (v1.1.0) that rewrites __dirname and __filename to static values at compile time, preserving original source file paths in bundled output. Stable, no recent updates since 2022. Different from other solutions (e.g., webpack.DefinePlugin) as it works specifically with Babel for transpilation, ensuring that after bundling, __dirname and __filename reflect the source file location rather than the output directory. Suitable for Node.js packages distributed as compiled code.
npm install babel-plugin-transform-dirname-filenameVerified import paths — ran on the pinned version, not inferred.
Shows installation, Babel config, and expected behavior: __dirname/__filename are rewritten to source file paths.
For Babel 7, install @babel/plugin-transform-dirname-filename or use the community fork.
Use relative paths or ensure the built output is consumed in an environment with the same source structure.
If you need to transform require paths, combine with other plugins like babel-plugin-module-resolver.
Use Babel 6 or switch to @babel/plugin-transform-dirname-filename for Babel 7 compatibility.
Validate .babelrc with a JSON validator or run npx babel --version to check config.