# unicorn/import-style

> Enforce how each module is imported: named, default, namespace, or for side effects

You can import most modules in several ways. If one file writes `path.join()` and the next writes `join()`, the same function has two names in your project, and a search finds only half of its uses. This rule lets you choose the import styles for a module and reports the others.

There are four styles:

| Style | Looks like |
| --- | --- |
| `named` | `import { join } from "node:path"` |
| `default` | `import path from "node:path"` |
| `namespace` | `import * as path from "node:path"` |
| `unassigned` | `import "node:path"` |

Without any options, `path`, `node:path` and `chalk` need a default import, and `util` and `node:util` need named imports. No other module is checked.

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/import-style" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"unicorn/import-style" = ["error", {
  styles = {},
  extendDefaultStyles = true,
  checkImport = true,
  checkDynamicImport = true,
  checkRequire = true,
  checkExportFrom = false,
}]
```

## Examples

Reported:

```js
import { join } from "node:path";
import util from "node:util";
```

Not reported:

```js
import path from "node:path";
import { promisify } from "node:util";
```

`require()` and `await import()` are checked too. `const path = require("path")` is accepted wherever a default import is. `const path = await import("path")` is a namespace import.

```js
const { join } = require("path");
const chalk = await import("chalk");
```

```js
const path = require("path");
const { default: chalk } = await import("chalk");
const { promisify } = await import("node:util");
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `styles` | `{}` | For each module name, the styles you allow (`true`) or forbid (`false`). `false` instead of an object turns off the check for that module. |
| `extendDefaultStyles` | `true` | Keep the default styles for `path`, `util` and `chalk`, and merge `styles` into them. |
| `checkImport` | `true` | Check `import` declarations. |
| `checkDynamicImport` | `true` | Check `import()`. |
| `checkRequire` | `true` | Check `require()`. |
| `checkExportFrom` | `false` | Also check `export ... from`. |

```toml bunfig.toml
[lint.rules]
"unicorn/import-style" = ["error", { styles = { react = { named = true } } }]
```

```js
import React from "react";
```

```js
import { useState } from "react";
```

Anything you set for `path`, `util` or `chalk` is added to the defaults. This allows a default import of `node:util` and still allows named imports. Add `"named": false` to forbid those.

```js
import util from "node:util";
import { promisify } from "node:util";
```

`false` turns a module off:

```js
import { join } from "node:path";
```

With `checkExportFrom`:

```js
export { join } from "node:path";
```
