import/no-unresolved

Require every import to point to a file or package that exists

Also called import-x/no-unresolved

A misspelled path, a file that moved, or a package that isn't installed only fails when you run or bundle the code. This rule looks up each import on disk and reports the ones it can't find.

Paths resolve the way they do in Bun and TypeScript:

  • A path without an extension is tried with the JavaScript and TypeScript extensions and with .json, then as a folder with an index file.
  • ./user.js also finds ./user.ts.
  • paths in your tsconfig.json are followed.
  • Anything after a ? is ignored.
  • Node.js built-in modules, bun and bun:test always resolve.
  • import type and export type aren't checked.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-unresolved" = "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-unresolved" = ["error", {
  esmodule = true,
  commonjs = false,
  amd = false,
  # ignore = <list of strings>,
  caseSensitive = true,
  caseSensitiveStrict = false,
}]

Examples#

Reported:

utils/format.js
export function formatDate() {}
example.js2 problems
import { formatDate } from "./utils/fromat.js";
import leftPad from "a-package-that-is-not-installed";

Not reported:

utils/format.js
export function formatDate() {}
example.jsNo problems
import fs from "node:fs";
import { formatDate } from "./utils/format.js";
import { formatDate as format } from "./utils/format";
user.ts
export const user = { name: "Ada" };
logo.svg
<svg />
example.tsNo problems
import { serve } from "bun";
import { test } from "bun:test";
import { user } from "./user";
import { user as sameUser } from "./user.ts";
import logoUrl from "./logo.svg?url";

The import/resolver setting changes how paths resolve. Two resolvers are supported: typescript, which is the default, and node with its extensions option. With an ESLint config the default is node, as in eslint-plugin-import. It tries .mjs, .js, .json and .node, and only knows the Node.js built-in modules.

bunfig.toml
[lint.settings]
"import/resolver" = "node"

Options#

OptionDefaultWhat it does
esmoduletrueCheck import, export .. from and import().
commonjsfalseAlso check require().
amdfalseAlso check the dependencies of define() and of require([..]).
ignore[]Regular expressions. A path that matches one of them is skipped.

caseSensitive and caseSensitiveStrict are accepted but have no effect.

Use ignore for paths that only a bundler understands:

bunfig.toml
[lint.rules]
"import/no-unresolved" = ["error", { ignore = ["^virtual:"] }]
example.jswith { "ignore": ["^virtual:"] }No problems
import routes from "virtual:routes";

When not to use it#

In a TypeScript project the type checker already reports imports it can't find.