noUndeclaredDependencies
此内容尚不支持你的语言。
Diagnostic Category: lint/correctness/noUndeclaredDependencies
Since: v1.6.0
Sources:
- Same as:
import/no-extraneous-dependencies
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 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 ExamplesInvalid
Section titled InvalidValid
Section titled ValidOptions
Section titled OptionsSince v2.0.0
This rule supports the following options:
devDependencies
: If set tofalse
, then the rule will show an error whendevDependencies
are imported. Defaults totrue
.peerDependencies
: If set tofalse
, then the rule will show an error whenpeerDependencies
are imported. Defaults totrue
.optionalDependencies
: If set tofalse
, then the rule will show an error whenoptionalDependencies
are imported. Defaults totrue
.
You can set the options like this:
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 the following example, only test files can use dependencies in devDependencies
section.
dependencies
, peerDependencies
, and optionalDependencies
are always available.