Registry / devops / paths.macro

paths.macro

JSON →
library3.0.1jsnpmunverified

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.

npm install paths.macro
INSTALL
IMPORT
SIG · PATHS.MACRO
P
paths.macro
devopsjavascriptv3.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (base)
✓ import base from 'paths.macro'
✗ const base = require('paths.macro')
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.
filename
✓ import { filename } from 'paths.macro'
✗ import filename from 'paths.macro'
Named export for the source file's name without extension. Must use destructured import, not default.
file
✓ import { file } from 'paths.macro'
Named export for the source file's name with extension (e.g., 'input.js').
fileAbsolute
✓ import { fileAbsolute } from 'paths.macro'
Named export for the absolute path of the source file (e.g., '/Users/you/project/src/input.js').
npmRoot
✓ import { npmRoot } from 'paths.macro'
Named export for the nearest npm root directory (where package.json is located).
gitRoot
✓ import { gitRoot } from 'paths.macro'
Named export for the nearest git root directory (where .git is located).

Shows how to import and log multiple path macros from a source file. The values are computed at build time.

// input.js import base, { filename, file, extension } from 'paths.macro'; console.log('base:', base); // "/src/" console.log('file:', file); // "input.js" console.log('filename:', filename); // "input" console.log('ext:', extension); // ".js"
Debug
Known issues
gotchapaths.macro only works with babel-plugin-macros. Without it, imports will not be transformed and will likely cause runtime errors.
fix
Install and configure babel-plugin-macros according to the README.
affects: all
gotchaThe macro transforms source files at build time; the values are hardcoded strings, not runtime variables.
fix
Understand that these values cannot be changed programmatically. Use environment variables or dynamic resolution if runtime flexibility is needed.
affects: all
gotchaIn versions prior to 3.0.0, the default export was 'fileAbsolute' instead of 'base'. This is a breaking change.
fix
Update imports to use 'base' for the directory path, or import 'fileAbsolute' explicitly if needed.
affects: <3.0.0
deprecatedThe 'wd' export (working directory) is deprecated in v3. Use 'base' or 'npmRoot' instead.
fix
Replace 'wd' with the appropriate path export.
affects: >=3.0.0
Errors
Common errors & fixes
Error: Cannot find module 'paths.macro'
Missing macro dependency or babel not configured to transform macros.
fix
Install paths.macro, babel-plugin-macros, and add 'macros' to babel plugins array.
SyntaxError: Unexpected token export
Trying to use ESM import syntax without a bundler/transpiler like Babel.
fix
Ensure your project uses Babel with babel-plugin-macros configured to transform the macro imports.
TypeError: (0 , _pathsMacro.default) is not a function
CommonJS require syntax used for a macro import, which is not supported.
fix
Change const base = require('paths.macro') to import base from 'paths.macro'.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
babel-plugin-macrosrequiredRequired by all babel macros; must be installed and configured in babel config
Agent activity
8 hits · last 30 days
node
8
Resources
paths.macro — npm install paths.macro · libregistry