Skip to content

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 ES6 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"));

How to use

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

bash
oxlint --deny import/no-unassigned-import --import-plugin
json
{
  "plugins": ["import"],
  "rules": {
    "import/no-unassigned-import": "error"
  }
}

References

Released under the MIT License.