import/no-relative-parent-imports

Disallow relative imports from parent directories

Also called import-x/no-relative-parent-imports

An import from ../ ties a directory to its place in the project. Move the directory and the import breaks. With this rule on, a directory only depends on packages and on the files inside it.

To fix a report, move the file you import into the directory, or make it a package and import it by name.

Only imports that resolve to a file are reported. A path that doesn't exist is left to import/no-unresolved.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-relative-parent-imports" = "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-relative-parent-imports" = ["error", {
  esmodule = true,
  commonjs = false,
  amd = false,
  # ignore = <list of strings>,
}]

Examples#

Reported:

src/api.js
export function getUser() {}
src/pages/home.js1 problem
import { getUser } from "../api.js";

Not reported:

src/pages/header.js
export function Header() {}
src/pages/parts/footer.js
export function Footer() {}
src/pages/home.jsNo problems
import { getUser } from "@acme/api";
import { Header } from "./header.js";
import { Footer } from "./parts/footer.js";

Options#

OptionDefaultWhat it does
esmoduletrueCheck import, export .. from and import().
commonjsfalseAlso check require().
amdfalseAlso check the dependencies of define() and of require([..]).
ignore[]Regular expressions. A path that matches one of them is skipped.

With ignore, you can keep one shared directory reachable:

bunfig.toml
[lint.rules]
"import/no-relative-parent-imports" = ["error", {
  ignore = ["^\\.\\./shared/"],
}]
src/shared/format.js
export function formatDate() {}
src/pages/home.jswith { "ignore": ["^\\.\\./shared/"] }No problems
import { formatDate } from "../shared/format.js";

When not to use it#

Most projects share code between sibling directories through ../. This rule fits a project that's built as a strict tree and reaches shared code through packages.