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:
[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.
[lint.rules]
"import/no-relative-parent-imports" = ["error", {
esmodule = true,
commonjs = false,
amd = false,
# ignore = <list of strings>,
}]Examples#
Reported:
export function getUser() {}import { getUser } from "../api.js"Relative imports from parent directories are not allowed. Please either pass what you're importing through at runtime (dependency injection), move home.js to same directory as ../api.js or consider making ../api.js a package.;Not reported:
export function Header() {}export function Footer() {}import { getUser } from "@acme/api";
import { Header } from "./header.js";
import { Footer } from "./parts/footer.js";Options#
| Option | Default | What it does |
|---|---|---|
esmodule | true | Check import, export .. from and import(). |
commonjs | false | Also check require(). |
amd | false | Also 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:
[lint.rules]
"import/no-relative-parent-imports" = ["error", {
ignore = ["^\\.\\./shared/"],
}]export function formatDate() {}{ "ignore": ["^\\.\\./shared/"] }No problemsimport { 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.