| 1 | # import/no-unresolved
|
|---|
| 2 |
|
|---|
| 3 | 💼 This rule is enabled in the following configs: ❗ `errors`, ☑️ `recommended`.
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Ensures an imported module can be resolved to a module on the local filesystem,
|
|---|
| 8 | as defined by standard Node `require.resolve` behavior.
|
|---|
| 9 |
|
|---|
| 10 | See [settings](../../README.md#settings) for customization options for the resolution (i.e.
|
|---|
| 11 | additional filetypes, `NODE_PATH`, etc.)
|
|---|
| 12 |
|
|---|
| 13 | This rule can also optionally report on unresolved modules in CommonJS `require('./foo')` calls and AMD `require(['./foo'], function (foo) {...})` and `define(['./foo'], function (foo) {...})`.
|
|---|
| 14 |
|
|---|
| 15 | To enable this, send `{ commonjs: true/false, amd: true/false }` as a rule option.
|
|---|
| 16 | Both are disabled by default.
|
|---|
| 17 |
|
|---|
| 18 | If you are using Webpack, see the section on [resolvers](../../README.md#resolvers).
|
|---|
| 19 |
|
|---|
| 20 | ## Rule Details
|
|---|
| 21 |
|
|---|
| 22 | ### Options
|
|---|
| 23 |
|
|---|
| 24 | By default, only ES6 imports will be resolved:
|
|---|
| 25 |
|
|---|
| 26 | ```js
|
|---|
| 27 | /*eslint import/no-unresolved: 2*/
|
|---|
| 28 | import x from './foo' // reports if './foo' cannot be resolved on the filesystem
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | If `{commonjs: true}` is provided, single-argument `require` calls will be resolved:
|
|---|
| 32 |
|
|---|
| 33 | ```js
|
|---|
| 34 | /*eslint import/no-unresolved: [2, { commonjs: true }]*/
|
|---|
| 35 | const { default: x } = require('./foo') // reported if './foo' is not found
|
|---|
| 36 |
|
|---|
| 37 | require(0) // ignored
|
|---|
| 38 | require(['x', 'y'], function (x, y) { /*...*/ }) // ignored
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | Similarly, if `{ amd: true }` is provided, dependency paths for `define` and `require`
|
|---|
| 42 | calls will be resolved:
|
|---|
| 43 |
|
|---|
| 44 | ```js
|
|---|
| 45 | /*eslint import/no-unresolved: [2, { amd: true }]*/
|
|---|
| 46 | define(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found
|
|---|
| 47 | require(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found
|
|---|
| 48 |
|
|---|
| 49 | const { default: x } = require('./foo') // ignored
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | Both may be provided, too:
|
|---|
| 53 |
|
|---|
| 54 | ```js
|
|---|
| 55 | /*eslint import/no-unresolved: [2, { commonjs: true, amd: true }]*/
|
|---|
| 56 | const { default: x } = require('./foo') // reported if './foo' is not found
|
|---|
| 57 | define(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found
|
|---|
| 58 | require(['./foo'], function (foo) { /*...*/ }) // reported if './foo' is not found
|
|---|
| 59 | ```
|
|---|
| 60 |
|
|---|
| 61 | #### `ignore`
|
|---|
| 62 |
|
|---|
| 63 | This rule has its own ignore list, separate from [`import/ignore`]. This is because you may want to know whether a module can be located, regardless of whether it can be parsed for exports: `node_modules`, CoffeeScript files, etc. are all good to resolve properly, but will not be parsed if configured as such via [`import/ignore`].
|
|---|
| 64 |
|
|---|
| 65 | To suppress errors from files that may not be properly resolved by your [resolver settings](../../README.md#resolver-plugins), you may add an `ignore` key with an array of `RegExp` pattern strings:
|
|---|
| 66 |
|
|---|
| 67 | ```js
|
|---|
| 68 | /*eslint import/no-unresolved: [2, { ignore: ['\\.img$'] }]*/
|
|---|
| 69 |
|
|---|
| 70 | import { x } from './mod' // may be reported, if not resolved to a module
|
|---|
| 71 |
|
|---|
| 72 | import coolImg from '../../img/coolImg.img' // will not be reported, even if not found
|
|---|
| 73 | ```
|
|---|
| 74 |
|
|---|
| 75 | #### `caseSensitive`
|
|---|
| 76 |
|
|---|
| 77 | By default, this rule will report paths whose case do not match the underlying filesystem path, if the FS is not case-sensitive. To disable this behavior, set the `caseSensitive` option to `false`.
|
|---|
| 78 |
|
|---|
| 79 | ```js
|
|---|
| 80 | /*eslint import/no-unresolved: [2, { caseSensitive: true (default) | false }]*/
|
|---|
| 81 | const { default: x } = require('./foo') // reported if './foo' is actually './Foo' and caseSensitive: true
|
|---|
| 82 | ```
|
|---|
| 83 |
|
|---|
| 84 | #### `caseSensitiveStrict`
|
|---|
| 85 |
|
|---|
| 86 | The `caseSensitive` option does not detect case for the current working directory. The `caseSensitiveStrict` option allows checking `cwd` in resolved path. By default, the option is disabled.
|
|---|
| 87 |
|
|---|
| 88 | ```js
|
|---|
| 89 | /*eslint import/no-unresolved: [2, { caseSensitiveStrict: true }]*/
|
|---|
| 90 |
|
|---|
| 91 | // Absolute paths
|
|---|
| 92 | import Foo from `/Users/fOo/bar/file.js` // reported, /Users/foo/bar/file.js
|
|---|
| 93 | import Foo from `d:/fOo/bar/file.js` // reported, d:/foo/bar/file.js
|
|---|
| 94 |
|
|---|
| 95 | // Relative paths, cwd is Users/foo/
|
|---|
| 96 | import Foo from `./../fOo/bar/file.js` // reported
|
|---|
| 97 | ```
|
|---|
| 98 |
|
|---|
| 99 | ## When Not To Use It
|
|---|
| 100 |
|
|---|
| 101 | If you're using a module bundler other than Node or Webpack, you may end up with a lot of false positive reports of missing dependencies.
|
|---|
| 102 |
|
|---|
| 103 | ## Further Reading
|
|---|
| 104 |
|
|---|
| 105 | - [Resolver plugins](../../README.md#resolvers)
|
|---|
| 106 | - [Node resolver](https://npmjs.com/package/eslint-import-resolver-node) (default)
|
|---|
| 107 | - [Webpack resolver](https://npmjs.com/package/eslint-import-resolver-webpack)
|
|---|
| 108 | - [`import/ignore`] global setting
|
|---|
| 109 |
|
|---|
| 110 | [`import/ignore`]: ../../README.md#importignore
|
|---|