| 1 | # import/no-commonjs
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Reports `require([string])` function calls. Will not report if >1 argument,
|
|---|
| 6 | or single argument is not a literal string.
|
|---|
| 7 |
|
|---|
| 8 | Reports `module.exports` or `exports.*`, also.
|
|---|
| 9 |
|
|---|
| 10 | Intended for temporary use when migrating to pure ES6 modules.
|
|---|
| 11 |
|
|---|
| 12 | ## Rule Details
|
|---|
| 13 |
|
|---|
| 14 | This will be reported:
|
|---|
| 15 |
|
|---|
| 16 | ```js
|
|---|
| 17 | var mod = require('./mod')
|
|---|
| 18 | , common = require('./common')
|
|---|
| 19 | , fs = require('fs')
|
|---|
| 20 | , whateverModule = require('./not-found')
|
|---|
| 21 |
|
|---|
| 22 | module.exports = { a: "b" }
|
|---|
| 23 | exports.c = "d"
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ### Allow require
|
|---|
| 27 |
|
|---|
| 28 | If `allowRequire` option is set to `true`, `require` calls are valid:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | /*eslint no-commonjs: [2, { allowRequire: true }]*/
|
|---|
| 32 | var mod = require('./mod');
|
|---|
| 33 | ```
|
|---|
| 34 |
|
|---|
| 35 | but `module.exports` is reported as usual.
|
|---|
| 36 |
|
|---|
| 37 | ### Allow conditional require
|
|---|
| 38 |
|
|---|
| 39 | By default, conditional requires are allowed:
|
|---|
| 40 |
|
|---|
| 41 | ```js
|
|---|
| 42 | var a = b && require("c")
|
|---|
| 43 |
|
|---|
| 44 | if (typeof window !== "undefined") {
|
|---|
| 45 | require('that-ugly-thing');
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | var fs = null;
|
|---|
| 49 | try {
|
|---|
| 50 | fs = require("fs")
|
|---|
| 51 | } catch (error) {}
|
|---|
| 52 | ```
|
|---|
| 53 |
|
|---|
| 54 | If the `allowConditionalRequire` option is set to `false`, they will be reported.
|
|---|
| 55 |
|
|---|
| 56 | If you don't rely on synchronous module loading, check out [dynamic import](https://github.com/airbnb/babel-plugin-dynamic-import-node).
|
|---|
| 57 |
|
|---|
| 58 | ### Allow primitive modules
|
|---|
| 59 |
|
|---|
| 60 | If `allowPrimitiveModules` option is set to `true`, the following is valid:
|
|---|
| 61 |
|
|---|
| 62 | ```js
|
|---|
| 63 | /*eslint no-commonjs: [2, { allowPrimitiveModules: true }]*/
|
|---|
| 64 |
|
|---|
| 65 | module.exports = "foo"
|
|---|
| 66 | module.exports = function rule(context) { return { /* ... */ } }
|
|---|
| 67 | ```
|
|---|
| 68 |
|
|---|
| 69 | but this is still reported:
|
|---|
| 70 |
|
|---|
| 71 | ```js
|
|---|
| 72 | /*eslint no-commonjs: [2, { allowPrimitiveModules: true }]*/
|
|---|
| 73 |
|
|---|
| 74 | module.exports = { x: "y" }
|
|---|
| 75 | exports.z = function boop() { /* ... */ }
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | This is useful for things like ESLint rule modules, which must export a function as
|
|---|
| 79 | the module.
|
|---|
| 80 |
|
|---|
| 81 | ## When Not To Use It
|
|---|
| 82 |
|
|---|
| 83 | If you don't mind mixing module systems (sometimes this is useful), you probably
|
|---|
| 84 | don't want this rule.
|
|---|
| 85 |
|
|---|
| 86 | It is also fairly noisy if you have a larger codebase that is being transitioned
|
|---|
| 87 | from CommonJS to ES6 modules.
|
|---|
| 88 |
|
|---|
| 89 | ## Contributors
|
|---|
| 90 |
|
|---|
| 91 | Special thanks to @xjamundx for donating the module.exports and exports.* bits.
|
|---|
| 92 |
|
|---|
| 93 | ## Further Reading
|
|---|
| 94 |
|
|---|
| 95 | - [`no-amd`](./no-amd.md): report on AMD `require`, `define`
|
|---|
| 96 | - Source: <https://github.com/xjamundx/eslint-plugin-modules>
|
|---|