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"Do not import modules using an absolute path;
const logger = require("/home/ana/app/src/logger.js"Do not import modules using an absolute path);example.jsNo problems
import config from "./config.js";
import express from "express";
const logger = require("../lib/logger.js");Options#
| Option | Default | What it does |
|---|---|---|
esmodule | true | Check import, export ... from and import(). |
commonjs | true | Check require(). |
amd | false | Check 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 problemsconst logger = require("/home/ana/app/src/logger.js");example.jswith
{ "amd": true }1 problemdefine(["/home/ana/app/src/logger.js"Do not import modules using an absolute path], 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 problemsimport logo from "/assets/logo.svg";