import/no-relative-packages

Disallow relative imports that reach into another package

fixable

Also called import-x/no-relative-packages

In a monorepo, ../ui/button.js only works while both packages stay where they are on disk. It also skips the other package's exports, and the dependency never shows up in your package.json.

A file belongs to the closest package.json above it that has a name. The rule reports a relative import that resolves to a file in a package with a different name.

--fix rewrites the path so it starts with the package's name.

Configure#

Off by default. To turn it on:

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

Examples#

Reported:

packages/app/package.json
{ "name": "app" }
packages/ui/package.json
{ "name": "@acme/ui" }
packages/ui/button.js
export function Button() {}
packages/app/index.js1 problem
import { Button } from "../ui/button.js";

Not reported:

packages/app/theme.js
export const theme = {};
packages/app/index.jsNo problems
import { Button } from "@acme/ui/button.js";
import { theme } from "./theme.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.
bunfig.toml
[lint.rules]
"import/no-relative-packages" = ["error", { commonjs = true }]