noUndeclaredDependencies
Summary
Section titled “Summary”- Rule available since:
v1.6.0 - Diagnostic Category:
lint/correctness/noUndeclaredDependencies - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noUndeclaredDependencies": "error" } } }}Description
Section titled “Description”Disallow the use of dependencies that aren’t specified in the package.json.
Indirect dependencies will trigger the rule because they aren’t declared in the package.json.
This means that if the package @org/foo has a dependency on lodash, and then you use
import "lodash" somewhere in your project, the rule will trigger a diagnostic for this import.
The rule is meant to catch those dependencies that aren’t declared inside the closest package.json, and
isn’t meant to detect dependencies declared in other manifest files, e.g. the root package.json in a monorepo setting.
The rule ignores imports that are not valid package names.
This includes internal imports that start with # and @/ and imports with a protocol such as node:, bun:, jsr:, https:.
To ensure that Visual Studio Code uses relative imports when it automatically imports a variable,
you may set javascript.preferences.importModuleSpecifier and typescript.preferences.importModuleSpecifier to relative.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”{ "dependencies": {}}import "vite";/index.js:1:8 lint/correctness/noUndeclaredDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Dependency vite isn’t specified in /package.json.
> 1 │ import “vite”;
│ ^^^^^^
2 │
ℹ This could lead to errors.
ℹ Add the dependency in your manifest.
{ "dependencies": { "vite": "*" }}import "vite"; // package is correctly declared
import assert from "node:assert"; // Node imports don't need declaration
import { A } from "./local.js"; // relative imports don't trigger the ruleimport { B } from "#alias"; // same goes for aliasesOptions
Section titled “Options”This rule supports the following options:
devDependencies: If set tofalse, then the rule will show an error whendevDependenciesare imported. Defaults totrue.peerDependencies: If set tofalse, then the rule will show an error whenpeerDependenciesare imported. Defaults totrue.optionalDependencies: If set tofalse, then the rule will show an error whenoptionalDependenciesare imported. Defaults totrue.
You can set the options like this:
{ "linter": { "rules": { "correctness": { "noUndeclaredDependencies": { "options": { "devDependencies": false, "peerDependencies": false, "optionalDependencies": false } } } } }}You can also use an array of globs instead of literal booleans.
When using an array of globs, the setting will be set to true (no errors reported)
if the name of the file being linted (i.e. not the imported file/module) matches a single glob
in the array, and false otherwise.
Example using the devDependencies option
Section titled “Example using the devDependencies option”In this example, only test files can use dependencies in the
devDependencies section. dependencies, peerDependencies, and
optionalDependencies are always available.
{ "linter": { "rules": { "correctness": { "noUndeclaredDependencies": { "options": { "devDependencies": [ "**/tests/*.test.js", "**/tests/*.spec.js" ] } } } } }}{ "devDependencies": { "vite": "*" }}// cannot import from a non-test fileimport "vite";/src/index.js:2:8 lint/correctness/noUndeclaredDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Dependency vite isn’t specified in /package.json.
1 │ // cannot import from a non-test file
> 2 │ import “vite”;
│ ^^^^^^
3 │
ℹ vite is part of your devDependencies, but it’s not intended to be used in this file.
ℹ You may want to consider moving it to the dependencies section.
// this works, because the file matches a glob from the optionsimport "vite";Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.