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

{/* 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]
"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,
}]
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```js
import { join } from "node:path";
import util from "node:util";
```

Not reported:

{/* good */}
```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.

{/* bad {"count": 2} */}
```js
const { join } = require("path");
const chalk = await import("chalk");
```

{/* good */}
```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 } } }]
```

{/* bad {"options": [{"styles": {"react": {"named": true}}}]} */}
```js
import React from "react";
```

{/* good {"options": [{"styles": {"react": {"named": true}}}]} */}
```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.

{/* good {"options": [{"styles": {"node:util": {"default": true}}}]} */}
```js
import util from "node:util";
import { promisify } from "node:util";
```

`false` turns a module off:

{/* good {"options": [{"styles": {"node:path": false}}]} */}
```js
import { join } from "node:path";
```

With `checkExportFrom`:

{/* bad {"options": [{"checkExportFrom": true}]} */}
```js
export { join } from "node:path";
```
