import/no-self-import

Disallow a module from importing itself

Also called import-x/no-self-import

A file that imports itself is almost always a mistake left over from a rename, a move, or an editor's auto-import. Everything it gets that way is already in scope, and require() returns the exports object before the file has finished filling it.

The rule resolves each path, so it also catches other spellings of the same file, such as . in an index.js. It checks import, export .. from, import() and require().

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-self-import" = "error" # or "warn"

Examples#

cart.js1 problem
import * as cart from "./cart.js";

export function total(items) {
  return items.length;
}

export function print(items) {
  console.log(cart.total(items));
}
index.js1 problem
const { version } = require("."); // undefined

exports.version = "1.0.0";
cart.jsNo problems
export function total(items) {
  return items.length;
}

export function print(items) {
  console.log(total(items));
}