import/no-absolute-path

Disallow importing a module by an absolute file system path

fixable

Also called import-x/no-absolute-path

An absolute path such as /home/ana/app/src/config.js only exists on the machine where it was written. The import breaks for everyone else who clones the project, in CI, and in production.

--fix rewrites the path to one that's relative to the importing file.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-absolute-path" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"import/no-absolute-path" = ["error", {
  esmodule = true,
  commonjs = true,
  amd = false,
  # ignore = <list of strings>,
}]

Examples#

example.js2 problems
import config from "/home/ana/app/src/config.js";

const logger = require("/home/ana/app/src/logger.js");
example.jsNo problems
import config from "./config.js";
import express from "express";

const logger = require("../lib/logger.js");

Options#

OptionDefaultWhat it does
esmoduletrueCheck import, export ... from and import().
commonjstrueCheck require().
amdfalseCheck the dependency lists of AMD define() and require() calls.
ignore[]Regular expressions. A path that matches one of them isn't checked.
example.jswith { "commonjs": false }No problems
const logger = require("/home/ana/app/src/logger.js");
example.jswith { "amd": true }1 problem
define(["/home/ana/app/src/logger.js"], function (logger) {
  logger.info("ready");
});

When not to use it#

Some dev servers and bundlers read a path that starts with / as relative to the project or its public folder. Exempt those paths with ignore, or leave the rule off.

bunfig.toml
[lint.rules]
"import/no-absolute-path" = ["error", { ignore = ["^/assets/"] }]
example.jswith { "ignore": ["^/assets/"] }No problems
import logo from "/assets/logo.svg";