parcel-bundler/parcel · error · TypeError

`origin` must be an absolute path

Error message

`origin` must be an absolute path

What it means

Thrown by relativePathForRequire in eslint-plugin/@parcel dev utils when opts.origin is not an absolute path. The function computes the relative path from the originating file to a required module and assumes origin is absolute; a relative origin would produce wrong results, so it rejects with a TypeError.

Source

Thrown at packages/dev/eslint-plugin/src/utils.js:65

    node.arguments &&
    node.arguments[0] &&
    typeof node.arguments[0].value === 'string' &&
    node.arguments[0].value.trim()
  );
}

/**
 * Like path.relative, but for determining relative path between a filepath and
 * a module it requests in a `require`.
 * @param {Object} opts
 * @param {string} opts.origin - Originating file. Must be an absolute path.
 * @param {string} opts.request - Requested module in the require. e.g. @parcel/core
 * @param {string} opts.pkgName - name of the package in the package json
 * @param {string} opts.pkgPath - path to the package.json
 */
function relativePathForRequire({origin, request, pkgName, pkgPath}) {
  if (!path.isAbsolute(origin)) {
    throw new TypeError('`origin` must be an absolute path');
  }

  if (path.isAbsolute(request)) {
    return request;
  }

  const pkgRoot = path.dirname(pkgPath);
  let relative = path
    .relative(
      path.dirname(origin),
      request.replace(new RegExp('^' + pkgName), pkgRoot),
    )
    // `require` expects unix-style '/' separators, but `path.relative` will respect
    // `path.sep`.
    .split(path.sep)
    .join('/');

  // prefer `require('../')` over `require('..')`

View on GitHub (pinned to 59484858a1)

Solutions

  1. Pass an absolute path for origin: `path.resolve(origin)` before calling.
  2. Ensure upstream callers (eslint context.getFilename()) provide absolute paths — they normally do.
  3. Add a guard: `if (!path.isAbsolute(origin)) origin = path.resolve(process.cwd(), origin);`.

Example fix

// before
relativePathForRequire({ origin: 'src/file.js', request, pkgName, pkgPath });

// after
relativePathForRequire({
  origin: path.resolve('src/file.js'),
  request, pkgName, pkgPath,
});
Defensive patterns

Strategy: validation

Validate before calling

const path = require('path');
function relativePathForRequireSafe(opts) {
  const origin = path.isAbsolute(opts.origin) ? opts.origin : path.resolve(opts.origin);
  return relativePathForRequire({ ...opts, origin });
}

Type guard

function isAbsolutePath(p: string): boolean {
  return typeof p === 'string' && path.isAbsolute(p);
}

Prevention

When it happens

Trigger: Calling relativePathForRequire({ origin: './src/file.js', ... }) — i.e. the eslint rule helper is invoked with a relative originating file path instead of an absolute one.

Common situations: Custom eslint rules built on @parcel/eslint-plugin utils passing __filename-relative paths; mock/test harnesses that synthesize relative origins; refactors that dropped a path.resolve() call.

Related errors


AI-assisted analysis of parcel-bundler/parcel@59484858a1 (2026-08-13). Data as JSON: /api/errors/fee627b5dec8f60c. Report an issue: GitHub.