Saltearse al contenido

noUndeclaredDependencies

Esta página aún no está disponible en tu idioma.

biome.json
{
"linter": {
"rules": {
"correctness": {
"noUndeclaredDependencies": "error"
}
}
}
}

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.

{
"dependencies": {}
}
import "vite";
{
"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 rule
import { B } from "#alias"; // same goes for aliases

This rule supports the following options:

  • devDependencies: If set to false, then the rule will show an error when devDependencies are imported. Defaults to true.
  • peerDependencies: If set to false, then the rule will show an error when peerDependencies are imported. Defaults to true.
  • optionalDependencies: If set to false, then the rule will show an error when optionalDependencies are imported. Defaults to true.

You can set the options like this:

biome.json
{
"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.

In this example, only test files can use dependencies in the devDependencies section. dependencies, peerDependencies, and optionalDependencies are always available.

biome.json
{
"linter": {
"rules": {
"correctness": {
"noUndeclaredDependencies": {
"options": {
"devDependencies": [
"**/tests/*.test.js",
"**/tests/*.spec.js"
]
}
}
}
}
}
}

package.json

{
"devDependencies": {
"vite": "*"
}
}

src/index.js

// cannot import from a non-test file
import "vite";

tests/foo.test.js

// this works, because the file matches a glob from the options
import "vite";