| 1 | # import/no-import-module-exports
|
|---|
| 2 |
|
|---|
| 3 | 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Reports the use of import declarations with CommonJS exports in any module
|
|---|
| 8 | except for the [main module](https://docs.npmjs.com/files/package.json#main).
|
|---|
| 9 |
|
|---|
| 10 | If you have multiple entry points or are using `js:next` this rule includes an
|
|---|
| 11 | `exceptions` option which you can use to exclude those files from the rule.
|
|---|
| 12 |
|
|---|
| 13 | ## Options
|
|---|
| 14 |
|
|---|
| 15 | ### `exceptions`
|
|---|
| 16 |
|
|---|
| 17 | - An array of globs. The rule will be omitted from any file that matches a glob
|
|---|
| 18 | in the options array. For example, the following setting will omit the rule
|
|---|
| 19 | in the `some-file.js` file.
|
|---|
| 20 |
|
|---|
| 21 | ```json
|
|---|
| 22 | "import/no-import-module-exports": ["error", {
|
|---|
| 23 | "exceptions": ["**/*/some-file.js"]
|
|---|
| 24 | }]
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | ## Rule Details
|
|---|
| 28 |
|
|---|
| 29 | ### Fail
|
|---|
| 30 |
|
|---|
| 31 | ```js
|
|---|
| 32 | import { stuff } from 'starwars'
|
|---|
| 33 | module.exports = thing
|
|---|
| 34 |
|
|---|
| 35 | import * as allThings from 'starwars'
|
|---|
| 36 | exports.bar = thing
|
|---|
| 37 |
|
|---|
| 38 | import thing from 'other-thing'
|
|---|
| 39 | exports.foo = bar
|
|---|
| 40 |
|
|---|
| 41 | import thing from 'starwars'
|
|---|
| 42 | const baz = module.exports = thing
|
|---|
| 43 | console.log(baz)
|
|---|
| 44 | ```
|
|---|
| 45 |
|
|---|
| 46 | ### Pass
|
|---|
| 47 |
|
|---|
| 48 | Given the following package.json:
|
|---|
| 49 |
|
|---|
| 50 | ```json
|
|---|
| 51 | {
|
|---|
| 52 | "main": "lib/index.js",
|
|---|
| 53 | }
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | import thing from 'other-thing'
|
|---|
| 58 | export default thing
|
|---|
| 59 |
|
|---|
| 60 | const thing = require('thing')
|
|---|
| 61 | module.exports = thing
|
|---|
| 62 |
|
|---|
| 63 | const thing = require('thing')
|
|---|
| 64 | exports.foo = bar
|
|---|
| 65 |
|
|---|
| 66 | import thing from 'otherthing'
|
|---|
| 67 | console.log(thing.module.exports)
|
|---|
| 68 |
|
|---|
| 69 | // in lib/index.js
|
|---|
| 70 | import foo from 'path';
|
|---|
| 71 | module.exports = foo;
|
|---|
| 72 |
|
|---|
| 73 | // in some-file.js
|
|---|
| 74 | // eslint import/no-import-module-exports: ["error", {"exceptions": ["**/*/some-file.js"]}]
|
|---|
| 75 | import foo from 'path';
|
|---|
| 76 | module.exports = foo;
|
|---|
| 77 | ```
|
|---|
| 78 |
|
|---|
| 79 | ### Further Reading
|
|---|
| 80 |
|
|---|
| 81 | - [webpack issue #4039](https://github.com/webpack/webpack/issues/4039)
|
|---|