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:

bunfig.toml
[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.

bunfig.toml
[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#

example.js3 problems
import "./register-globals.js";
import {} from "lodash";
require("./patch-console.js");
example.jsNo problems
import { registerGlobals } from "./register-globals.js";
const patchConsole = require("./patch-console.js");

registerGlobals();
patchConsole();

Options#

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

bunfig.toml
[lint.rules]
"import/no-unassigned-import" = ["error", {
  allow = ["**/*.css", "reflect-metadata"],
}]

With this configuration, these are allowed:

example.jswith { "allow": ["**/*.css", "reflect-metadata"] }No problems
import "./app.css";
import "reflect-metadata";

And this is still reported:

example.jswith { "allow": ["**/*.css", "reflect-metadata"] }1 problem
import "./register-globals.js";

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