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

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

Fix frontend appearance

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