source: frontend/node_modules/eslint-plugin-import/docs/rules/no-extraneous-dependencies.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.8 KB
Line 
1# import/no-extraneous-dependencies
2
3<!-- end auto-generated rule header -->
4
5Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, or `bundledDependencies`.
6The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. This behavior can be changed with the rule option `packageDir`. Normally ignores imports of modules marked internal, but this can be changed with the rule option `includeInternal`. Type imports can be verified by specifying `includeTypes`.
7
8Modules have to be installed for this rule to work.
9
10## Options
11
12This rule supports the following options:
13
14`devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`.
15Type imports are ignored by default.
16
17`optionalDependencies`: If set to `false`, then the rule will show an error when `optionalDependencies` are imported. Defaults to `true`.
18
19`peerDependencies`: If set to `false`, then the rule will show an error when `peerDependencies` are imported. Defaults to `true`.
20
21`bundledDependencies`: If set to `false`, then the rule will show an error when `bundledDependencies` are imported. Defaults to `true`.
22
23You can set the options like this:
24
25```js
26"import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}]
27```
28
29You can also use an array of globs instead of literal booleans:
30
31```js
32"import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.js", "**/*.spec.js"]}]
33```
34
35When 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.
36
37There are 2 boolean options to opt into checking extra imports that are normally ignored: `includeInternal`, which enables the checking of internal modules, and `includeTypes`, which enables checking of type imports in TypeScript.
38
39```js
40"import/no-extraneous-dependencies": ["error", {"includeInternal": true, "includeTypes": true}]
41```
42
43Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json.
44
45If provided as a relative path string, will be computed relative to the current working directory at linter execution time. If this is not ideal (does not work with some editor integrations), consider using `__dirname` to provide a path relative to your configuration.
46
47```js
48"import/no-extraneous-dependencies": ["error", {"packageDir": './some-dir/'}]
49// or
50"import/no-extraneous-dependencies": ["error", {"packageDir": path.join(__dirname, 'some-dir')}]
51```
52
53It may also be an array of multiple paths, to support monorepos or other novel project
54folder layouts:
55
56```js
57"import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
58```
59
60## Rule Details
61
62Given the following `package.json`:
63
64```json
65{
66 "name": "my-project",
67 "...": "...",
68 "dependencies": {
69 "builtin-modules": "^1.1.1",
70 "lodash.cond": "^4.2.0",
71 "lodash.find": "^4.2.0",
72 "pkg-up": "^1.0.0"
73 },
74 "devDependencies": {
75 "ava": "^0.13.0",
76 "eslint": "^2.4.0",
77 "eslint-plugin-ava": "^1.3.0",
78 "xo": "^0.13.0"
79 },
80 "optionalDependencies": {
81 "lodash.isarray": "^4.0.0"
82 },
83 "peerDependencies": {
84 "react": ">=15.0.0 <16.0.0"
85 },
86 "bundledDependencies": [
87 "@generated/foo",
88 ]
89}
90```
91
92## Fail
93
94```js
95var _ = require('lodash');
96import _ from 'lodash';
97
98import react from 'react';
99
100/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */
101import test from 'ava';
102var test = require('ava');
103
104/* eslint import/no-extraneous-dependencies: ["error", {"optionalDependencies": false}] */
105import isArray from 'lodash.isarray';
106var isArray = require('lodash.isarray');
107
108/* eslint import/no-extraneous-dependencies: ["error", {"bundledDependencies": false}] */
109import foo from '"@generated/foo"';
110var foo = require('"@generated/foo"');
111
112/* eslint import/no-extraneous-dependencies: ["error", {"includeInternal": true}] */
113import foo from './foo';
114var foo = require('./foo');
115
116/* eslint import/no-extraneous-dependencies: ["error", {"includeTypes": true}] */
117import type { MyType } from 'foo';
118```
119
120## Pass
121
122```js
123// Builtin and internal modules are fine
124var path = require('path');
125var foo = require('./foo');
126
127import test from 'ava';
128import find from 'lodash.find';
129import isArray from 'lodash.isarray';
130import foo from '"@generated/foo"';
131import type { MyType } from 'foo';
132
133/* eslint import/no-extraneous-dependencies: ["error", {"peerDependencies": true}] */
134import react from 'react';
135```
136
137## When Not To Use It
138
139If you do not have a `package.json` file in your project.
Note: See TracBrowser for help on using the repository browser.