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 anindexfile. ./user.jsalso finds./user.ts.pathsin yourtsconfig.jsonare followed.- Anything after a
?is ignored. - Node.js built-in modules,
bunandbun:testalways resolve. import typeandexport typearen't checked.
Configure#
Off by default. To turn it on:
[lint.rules]
"import/no-unresolved" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"import/no-unresolved" = ["error", {
esmodule = true,
commonjs = false,
amd = false,
# ignore = <list of strings>,
caseSensitive = true,
caseSensitiveStrict = false,
}]Examples#
Reported:
export function formatDate() {}import { formatDate } from "./utils/fromat.js"Unable to resolve path to module ./utils/fromat.js.;
import leftPad from "a-package-that-is-not-installed"Unable to resolve path to module a-package-that-is-not-installed.;Not reported:
export function formatDate() {}import fs from "node:fs";
import { formatDate } from "./utils/format.js";
import { formatDate as format } from "./utils/format";export const user = { name: "Ada" };<svg />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.
[lint.settings]
"import/resolver" = "node"Options#
| Option | Default | What it does |
|---|---|---|
esmodule | true | Check import, export .. from and import(). |
commonjs | false | Also check require(). |
amd | false | Also 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:
[lint.rules]
"import/no-unresolved" = ["error", { ignore = ["^virtual:"] }]{ "ignore": ["^virtual:"] }No problemsimport routes from "virtual:routes";When not to use it#
In a TypeScript project the type checker already reports imports it can't find.