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:
[lint.rules]
"unicorn/import-style" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"unicorn/import-style" = ["error", {
styles = {},
extendDefaultStyles = true,
checkImport = true,
checkDynamicImport = true,
checkRequire = true,
checkExportFrom = false,
}]Examples#
import { join } from "node:path";Use default import for module node:path.
import util from "node:util";Use named import for module node:util.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.
const { join } = require("path")Use namespace or default import for module path.;
const chalk = await import("chalk")Use default import for module chalk.;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. |
[lint.rules]
"unicorn/import-style" = ["error", { styles = { react = { named = true } } }]{ "styles": { "react": { "named": true } } }1 problemimport React from "react";Use named import for module react.{ "styles": { "react": { "named": true } } }No problemsimport { 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.
{ "styles": { "node:util": { "default": true } } }No problemsimport util from "node:util";
import { promisify } from "node:util";false turns a module off:
{ "styles": { "node:path": false } }No problemsimport { join } from "node:path";With checkExportFrom:
{ "checkExportFrom": true }1 problemexport { join } from "node:path";Use default import for module node:path.