{"id":20257,"library":"paths.macro","title":"Babel paths.macro","description":"A Babel macro that provides compile-time access to file path information like __dirname and __filename, returning static values rather than runtime variables. Current version 3.0.1. It uses babel-plugin-macros for integration. Key differentiators: no need for __filename polyfills in Node ESM, works with bundlers that support macros, and supports multiple path forms (base, file, extension, filename, npmRoot, gitRoot). Commonly used in tooling and build-time code generation.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","__dirname","__filename","babel","babel-plugin-macros","base","extention","file","filename","typescript"],"install":[{"cmd":"npm install paths.macro","lang":"bash","label":"npm"},{"cmd":"yarn add paths.macro","lang":"bash","label":"yarn"},{"cmd":"pnpm add paths.macro","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required by all babel macros; must be installed and configured in babel config","package":"babel-plugin-macros","optional":false}],"imports":[{"note":"Default import resolves to the base directory of the source file, e.g. '/src/'. CommonJS require does not work because it's a macro that must be transformed at build time.","wrong":"const base = require('paths.macro')","symbol":"default (base)","correct":"import base from 'paths.macro'"},{"note":"Named export for the source file's name without extension. Must use destructured import, not default.","wrong":"import filename from 'paths.macro'","symbol":"filename","correct":"import { filename } from 'paths.macro'"},{"note":"Named export for the source file's name with extension (e.g., 'input.js').","wrong":"","symbol":"file","correct":"import { file } from 'paths.macro'"},{"note":"Named export for the absolute path of the source file (e.g., '/Users/you/project/src/input.js').","wrong":"","symbol":"fileAbsolute","correct":"import { fileAbsolute } from 'paths.macro'"},{"note":"Named export for the nearest npm root directory (where package.json is located).","wrong":"","symbol":"npmRoot","correct":"import { npmRoot } from 'paths.macro'"},{"note":"Named export for the nearest git root directory (where .git is located).","wrong":"","symbol":"gitRoot","correct":"import { gitRoot } from 'paths.macro'"}],"quickstart":{"code":"// input.js\nimport base, { filename, file, extension } from 'paths.macro';\n\nconsole.log('base:', base);       // \"/src/\"\nconsole.log('file:', file);       // \"input.js\"\nconsole.log('filename:', filename); // \"input\"\nconsole.log('ext:', extension);    // \".js\"","lang":"javascript","note":"Ensure babel-plugin-macros is installed and enabled in your babel config.","description":"Shows how to import and log multiple path macros from a source file. The values are computed at build time."},"warnings":[{"fix":"Install and configure babel-plugin-macros according to the README.","message":"paths.macro only works with babel-plugin-macros. Without it, imports will not be transformed and will likely cause runtime errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Understand that these values cannot be changed programmatically. Use environment variables or dynamic resolution if runtime flexibility is needed.","message":"The macro transforms source files at build time; the values are hardcoded strings, not runtime variables.","severity":"gotcha","affected_versions":"all"},{"fix":"Update imports to use 'base' for the directory path, or import 'fileAbsolute' explicitly if needed.","message":"In versions prior to 3.0.0, the default export was 'fileAbsolute' instead of 'base'. This is a breaking change.","severity":"gotcha","affected_versions":"<3.0.0"},{"fix":"Replace 'wd' with the appropriate path export.","message":"The 'wd' export (working directory) is deprecated in v3. Use 'base' or 'npmRoot' instead.","severity":"deprecated","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":"Install paths.macro, babel-plugin-macros, and add 'macros' to babel plugins array.","cause":"Missing macro dependency or babel not configured to transform macros.","error":"Error: Cannot find module 'paths.macro'"},{"fix":"Ensure your project uses Babel with babel-plugin-macros configured to transform the macro imports.","cause":"Trying to use ESM import syntax without a bundler/transpiler like Babel.","error":"SyntaxError: Unexpected token export"},{"fix":"Change const base = require('paths.macro') to import base from 'paths.macro'.","cause":"CommonJS require syntax used for a macro import, which is not supported.","error":"TypeError: (0 , _pathsMacro.default) is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}