import/no-unassigned-import
Disallow imports that bind no name
Also called import-x/no-unassigned-import
import "./setup.js" runs a module only for its side effects. Nothing in the file shows what the module changes, and the order of these imports matters even though nothing says so. With this rule on, every import needs to bring in a name you can follow.
A require() call that's a statement by itself is reported too, and so is an import with empty braces.
Configure#
Off by default. To turn it on:
[lint.rules]
"import/no-unassigned-import" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"import/no-unassigned-import" = ["error", {
allow = [], # list of strings
# devDependencies = <true, false or list>,
# optionalDependencies = <true, false or list>,
# peerDependencies = <true, false or list>,
}]Examples#
import "./register-globals.js";Imported module should be assigned
import {} from "lodash";Imported module should be assigned
require("./patch-console.js")Imported module should be assigned;import { registerGlobals } from "./register-globals.js";
const patchConsole = require("./patch-console.js");
registerGlobals();
patchConsole();Options#
| Option | Default | What it does |
|---|---|---|
allow | [] | Globs for modules you can import for their side effects. |
A package name is matched exactly as you wrote it. A relative path is made absolute first, so a glob for files either starts with **/ or is relative to the directory you run bun lint in.
devDependencies, optionalDependencies and peerDependencies are accepted but have no effect.
[lint.rules]
"import/no-unassigned-import" = ["error", {
allow = ["**/*.css", "reflect-metadata"],
}]With this configuration, these are allowed:
{ "allow": ["**/*.css", "reflect-metadata"] }No problemsimport "./app.css";
import "reflect-metadata";And this is still reported:
{ "allow": ["**/*.css", "reflect-metadata"] }1 problemimport "./register-globals.js";Imported module should be assignedWhen not to use it#
Stylesheets, polyfills and test setup files are meant to be imported this way. If you'd end up listing most of your side-effect imports in allow, leave the rule off.