close

no-absolute-path

Added in v0.9.4

Configuration

rslint.config.ts
import { defineConfig, importPlugin } from '@rslint/core';

export default defineConfig([
  importPlugin.configs.recommended,
  {
    rules: {
      'import/no-absolute-path': 'error',
    },
  },
]);

Rule Details

Disallow absolute module paths, which tie imports to a particular filesystem layout. The rule checks imports, re-exports, dynamic imports, and direct require() calls with string literals.

Examples of incorrect code for this rule:

import value from '/project/lib/value.js';
export * from '/project/lib/value.js';
const other = require('/project/lib/other.js');
import('/project/lib/lazy.js');

Examples of correct code for this rule:

import value from './lib/value.js';
export * from 'some-package';
const other = require('../lib/other.js');
import('./lib/lazy.js');

The automatic fix replaces the absolute path with a double-quoted path relative to the importing file. Like upstream, absolute-path detection follows the host platform: paths starting with / are absolute on all platforms, while Windows drive paths such as C:/project/lib/value.js are only reported on Windows. Fixes use / as the path separator on all platforms.

Options

The rule accepts one object:

OptionDefaultMeaning
esmoduletrueCheck imports, re-exports, and dynamic imports.
commonjstrueCheck direct require() calls.
amdfalseCheck dependency arrays in two-argument define() and require() calls.
ignoreNoneSkip paths matching any supplied JavaScript regular expression string.

For example, { "commonjs": false, "amd": true } checks AMD dependencies while ignoring CommonJS calls:

define(['/project/lib/value.js'], function (value) {}); // Reported
require(['/project/lib/value.js'], function (value) {}); // Reported
const value = require('/project/lib/value.js'); // Ignored

For example, { "ignore": ["^/generated/"] } allows absolute paths beginning with /generated/. Patterns are case-sensitive and match the decoded path, including any escaped characters. When specified, ignore must contain at least one pattern, without duplicate entries.

Differences from upstream

  • Automatic fixes escape <, >, &, U+2028, and U+2029. For example, a path containing <a> becomes "./\u003ca\u003e" instead of "./<a>". The fixed paths have the same JavaScript value and refer to the same module.
  • Fixes for names beginning with a dot include the relative-path prefix. For example, importing /project/.hidden.js from /project/index.js becomes "./.hidden.js". Upstream produces ".hidden.js", which module loaders can interpret as a package name and fail to load.
  • Some ignore character ranges containing emoji are rejected. For example, use "[\\ud83d\\ude00-\\uFFFF]" instead of "[😀-\\uFFFF]"; upstream accepts both with the same matching behavior.
  • ignore rejects named capture groups reused across separate alternatives. For example, "(?:(?<x>/foo)|(?<x>/bar))" is rejected. Use "(?<x>/foo|/bar)" to ignore the same paths. Upstream accepts the former when its JavaScript runtime supports duplicate named capture groups.

Original Documentation