Skip to content
← Back to rules

import/no-absolute-path Suspicious

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does ​

Forbid the import of modules using absolute paths.

Why is this bad? ​

Node.js allows the import of modules using an absolute path such as /home/xyz/file.js. That is a bad practice as it ties the code using it to your computer, and therefore makes it unusable in packages distributed on npm for instance.

Examples ​

Examples of incorrect code for this rule:

js
import f from "/foo";
import f from "/some/path";
var f = require("/foo");
var f = require("/some/path");

Examples of correct code for this rule:

js
import _ from "lodash";
import foo from "foo";
import foo from "./foo";

var _ = require("lodash");
var foo = require("foo");
var foo = require("./foo");

Examples of incorrect code for the { amd: true } option:

js
define("/foo", function (foo) {});
require("/foo", function (foo) {});

Examples of correct code for the { amd: true } option:

js
define("./foo", function (foo) {});
require("./foo", function (foo) {});

Configuration ​

This rule accepts a configuration object with the following properties:

amd ​

type: boolean

default: false

If set to true, dependency paths for AMD-style define and require calls will be resolved:

js
/* import/no-absolute-path: ["error", { "commonjs": false, "amd": true }] */
define(["/foo"], function (foo) {
  /*...*/
}); // reported
require(["/foo"], function (foo) {
  /*...*/
}); // reported

const foo = require("/foo"); // ignored because of explicit `commonjs: false`

commonjs ​

type: boolean

default: true

If set to true, dependency paths for CommonJS-style require calls will be resolved:

js
var foo = require("/foo"); // reported

esmodule ​

type: boolean

default: true

If set to true, dependency paths for ES module import statements will be resolved:

js
import foo from "/foo"; // reported

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/no-absolute-path": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/no-absolute-path": "error",
  },
});
bash
oxlint --deny import/no-absolute-path --import-plugin

Version ​

This rule was added in v0.15.13.

References ​