| 1 | # import/no-extraneous-dependencies
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, or `bundledDependencies`.
|
|---|
| 6 | The 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 |
|
|---|
| 8 | Modules have to be installed for this rule to work.
|
|---|
| 9 |
|
|---|
| 10 | ## Options
|
|---|
| 11 |
|
|---|
| 12 | This 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`.
|
|---|
| 15 | Type 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 |
|
|---|
| 23 | You can set the options like this:
|
|---|
| 24 |
|
|---|
| 25 | ```js
|
|---|
| 26 | "import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}]
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | You 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 |
|
|---|
| 35 | 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.
|
|---|
| 36 |
|
|---|
| 37 | There 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 |
|
|---|
| 43 | Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json.
|
|---|
| 44 |
|
|---|
| 45 | If 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 |
|
|---|
| 53 | It may also be an array of multiple paths, to support monorepos or other novel project
|
|---|
| 54 | folder layouts:
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | "import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ## Rule Details
|
|---|
| 61 |
|
|---|
| 62 | Given 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
|
|---|
| 95 | var _ = require('lodash');
|
|---|
| 96 | import _ from 'lodash';
|
|---|
| 97 |
|
|---|
| 98 | import react from 'react';
|
|---|
| 99 |
|
|---|
| 100 | /* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */
|
|---|
| 101 | import test from 'ava';
|
|---|
| 102 | var test = require('ava');
|
|---|
| 103 |
|
|---|
| 104 | /* eslint import/no-extraneous-dependencies: ["error", {"optionalDependencies": false}] */
|
|---|
| 105 | import isArray from 'lodash.isarray';
|
|---|
| 106 | var isArray = require('lodash.isarray');
|
|---|
| 107 |
|
|---|
| 108 | /* eslint import/no-extraneous-dependencies: ["error", {"bundledDependencies": false}] */
|
|---|
| 109 | import foo from '"@generated/foo"';
|
|---|
| 110 | var foo = require('"@generated/foo"');
|
|---|
| 111 |
|
|---|
| 112 | /* eslint import/no-extraneous-dependencies: ["error", {"includeInternal": true}] */
|
|---|
| 113 | import foo from './foo';
|
|---|
| 114 | var foo = require('./foo');
|
|---|
| 115 |
|
|---|
| 116 | /* eslint import/no-extraneous-dependencies: ["error", {"includeTypes": true}] */
|
|---|
| 117 | import type { MyType } from 'foo';
|
|---|
| 118 | ```
|
|---|
| 119 |
|
|---|
| 120 | ## Pass
|
|---|
| 121 |
|
|---|
| 122 | ```js
|
|---|
| 123 | // Builtin and internal modules are fine
|
|---|
| 124 | var path = require('path');
|
|---|
| 125 | var foo = require('./foo');
|
|---|
| 126 |
|
|---|
| 127 | import test from 'ava';
|
|---|
| 128 | import find from 'lodash.find';
|
|---|
| 129 | import isArray from 'lodash.isarray';
|
|---|
| 130 | import foo from '"@generated/foo"';
|
|---|
| 131 | import type { MyType } from 'foo';
|
|---|
| 132 |
|
|---|
| 133 | /* eslint import/no-extraneous-dependencies: ["error", {"peerDependencies": true}] */
|
|---|
| 134 | import react from 'react';
|
|---|
| 135 | ```
|
|---|
| 136 |
|
|---|
| 137 | ## When Not To Use It
|
|---|
| 138 |
|
|---|
| 139 | If you do not have a `package.json` file in your project.
|
|---|