Registry / babel-root-import

babel-root-import

JSON →
library4.1.8jsnpmunverified

Babel plugin to enable root-relative imports (e.g., import Foo from '@/foo') in JavaScript/TypeScript projects. Version 4.1.8 is the legacy package, renamed to babel-plugin-root-import (latest v6.6.0). This plugin transforms root-relative paths to relative paths at build time. Key differentiator: simple configuration (rootPathSuffix) and support for dynamic imports. Often confused with webpack aliases but works at Babel level. Low release cadence: last update 2019. Consider using babel-plugin-root-import instead, which is the maintained successor.

npm install babel-root-import
INSTALL
IMPORT
SIG · BABEL-ROOT-IMPORT
B
babel-root-import
javascriptv4.1.8
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 (plugin)
✓ module.exports = { plugins: [['babel-root-import', { rootPathSuffix: 'src' }]] }
✗ module.exports = { plugins: ['babel-root-import'] }
Options must be passed as an array with the plugin name and options object. Missing options leads to default behavior (root = './'), which may not work as expected.
default (plugin) with .babelrc
✓ { "plugins": [["babel-root-import", { "rootPathSuffix": "src" }]] }
✗ { "plugins": ["babel-root-import"] }
In .babelrc JSON, the plugin and options must be an array in an array. Omitting options is common but may cause root resolution to fail.
Import syntax usage
✓ import Foo from '@/foo'
✗ import Foo from '/foo' or import Foo from '~/foo'
Default root prefix is '@/'. Other prefixes like '~' are not supported without custom configuration (rootPathPrefix).

Shows installation, .babelrc configuration with custom root suffix and prefix, and an example import resolving to a src-relative path.

// Install: npm install --save-dev babel-root-import // Configure in .babelrc: { "plugins": [ ["babel-root-import", { "rootPathSuffix": "src", "rootPathPrefix": "@/" }] ] } // Usage in src/app.js: import User from '@/models/user'; // resolves to ./src/models/user console.log(User);
Debug
Known issues
deprecatedPackage is renamed to babel-plugin-root-import. babel-root-import will no longer receive updates.
fix
Use babel-plugin-root-import instead. Requires uninstalling this package and installing the new one.
affects: >=4.1.8
breakingRoot path prefix changed default from '~' in early versions to '@/' in version 5+.
fix
Update prefix in code and configuration to '@/' or set a custom prefix.
affects: >=5.0.0
gotchaOptions object must be wrapped in an array in the plugins list. Common mistake: passing object directly.
fix
Use array syntax: ["babel-root-import", { rootPathSuffix: "src" }]
affects: >=4.0.0
gotchaThe plugin does not work without a rootPathSuffix; default is './' which may not resolve as expected.
fix
Set rootPathSuffix to your source directory (e.g., 'src') or adjust it appropriately.
affects: >=4.0.0
Errors
Common errors & fixes
Error: Cannot find module '@some/module'
Root import path not transformed correctly due to missing or misconfigured plugin options.
fix
Ensure babel-root-import is installed and configured with rootPathSuffix and rootPathPrefix in .babelrc or babel.config.js.
Module not found: Can't resolve '@/foo' in '/path/to/project'
Webpack or other bundler cannot resolve the @ alias; the plugin only transforms to relative paths at Babel level, but the bundler may need its own alias.
fix
Add a corresponding alias in webpack config (resolve.alias) or use a different root import approach.
SyntaxError: Unexpected token /
Using root import syntax (e.g., import from '@/foo') without Babel processing (e.g., running node directly).
fix
Ensure your code is transpiled with Babel before running, or use node with a require hook like @babel/register.
Upgrade
Version history
4.1.8latest on npm
Audit
Dependencies
babel-coreoptionalRequires Babel to function as a plugin
Agent activity
6 hits · last 30 days
node
6
Resources
babel-root-import — npm install babel-root-import · libregistry