babel-plugin-transform-relative-paths

raw JSON →
0.1.0 verified Sat Apr 25 auth: no javascript

A Babel plugin that transforms relative import/require paths by adding an extra directory level (e.g., './' becomes '../', '../' becomes '../../'). Current stable version: 0.1.0. Release cadence is unknown. Differentiator: simple, no-config path manipulation for monorepos or code generators, but limited to single-level up transformation without custom configuration.

error Error: Cannot find module 'babel-plugin-transform-relative-paths'
cause Plugin not installed or Babel cannot resolve it.
fix
npm install --save-dev babel-plugin-transform-relative-paths
error TypeError: (0 , _plugin.default) is not a function
cause Using ES module import syntax with a CJS plugin that does not export a default function.
fix
Use require('babel-plugin-transform-relative-paths') instead of import statement.
gotcha Plugin transforms all relative paths in import/require statements indiscriminately, including those from node_modules if not excluded.
fix Configure Babel to exclude node_modules via babel.config.js: module.exports = { plugins: [['transform-relative-paths']], only: ['./src'] };
gotcha Only supports moving paths up one level; cannot customize depth or transform in reverse.
fix For custom transformations, consider using a different plugin or writing a custom one.
gotcha Plugin does not handle dynamic requires or string concatenation in require() calls.
fix Ensure your require() calls use static string literals.
npm install babel-plugin-transform-relative-paths
yarn add babel-plugin-transform-relative-paths
pnpm add babel-plugin-transform-relative-paths

Demonstrates Babel plugin configuration and transformation of relative paths by adding one directory level.

// .babelrc
{
  "plugins": ["transform-relative-paths"]
}

// input.js
import someModule from './a-relative-path';
import someOtherModule from '../a-relative-path';
const someCommonModule = require('../../a-relative-path');

// output.js (after transformation)
import someModule from '../a-relative-path';
import someOtherModule from '../../a-relative-path';
const someCommonModule = require('../../../a-relative-path');