Skip to content
← Back to rules

import/no-unassigned-import Suspicious

What it does ​

This rule aims to remove modules with side-effects by reporting when a module is imported but not assigned.

Why is this bad? ​

With both CommonJS' require and the ES modules' import syntax, it is possible to import a module but not to use its result. This can be done explicitly by not assigning the module to a variable. Doing so can mean either of the following things:

  • The module is imported but not used
  • The module has side-effects. Having side-effects, makes it hard to know whether the module is actually used or can be removed. It can also make it harder to test or mock parts of your application.

Examples ​

Examples of incorrect code for this rule:

js
import "should";
require("should");

Examples of correct code for this rule:

js
import _ from "foo";
import _, { foo } from "foo";
import _, { foo as bar } from "foo";
const _ = require("foo");
const { foo } = require("foo");
const { foo: bar } = require("foo");
bar(require("foo"));

Configuration ​

This rule accepts a configuration object with the following properties:

allow ​

type: string[]

default: []

A list of glob patterns to allow unassigned imports for specific modules. For example: { "allow": ["**/*.css"] } will allow unassigned imports for any module ending with .css.

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/no-unassigned-import": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/no-unassigned-import": "error",
  },
});
bash
oxlint --deny import/no-unassigned-import --import-plugin

Version ​

This rule was added in v0.16.11.

References ​