# import/no-relative-parent-imports

> Disallow relative imports from parent directories

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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml 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.

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

{/* End of the generated section. */}

## Examples

Reported:

{/* file "src/api.js" */}
```js src/api.js
export function getUser() {}
```

{/* bad {"filename": "src/pages/home.js"} */}
```js src/pages/home.js
import { getUser } from "../api.js";
```

Not reported:

{/* file "src/pages/header.js" */}
```js src/pages/header.js
export function Header() {}
```

{/* file "src/pages/parts/footer.js" */}
```js src/pages/parts/footer.js
export function Footer() {}
```

{/* good {"filename": "src/pages/home.js"} */}
```js src/pages/home.js
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:

```toml bunfig.toml
[lint.rules]
"import/no-relative-parent-imports" = ["error", {
  ignore = ["^\\.\\./shared/"],
}]
```

{/* file "src/shared/format.js" */}
```js src/shared/format.js
export function formatDate() {}
```

{/* good {"filename": "src/pages/home.js", "options": [{"ignore": ["^\\.\\./shared/"]}]} */}
```js src/pages/home.js
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.
