]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: global-require | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
eb39fafa | 6 | |
f2a92ac6 | 7 | This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). |
56c4a2cb | 8 | |
eb39fafa DC |
9 | In Node.js, module dependencies are included using the `require()` function, such as: |
10 | ||
11 | ```js | |
12 | var fs = require("fs"); | |
13 | ``` | |
14 | ||
15 | While `require()` may be called anywhere in code, some style guides prescribe that it should be called only in the top level of a module to make it easier to identify dependencies. For instance, it's arguably harder to identify dependencies when they are deeply nested inside of functions and other statements: | |
16 | ||
17 | ```js | |
18 | function foo() { | |
eb39fafa DC |
19 | if (condition) { |
20 | var fs = require("fs"); | |
21 | } | |
22 | } | |
23 | ``` | |
24 | ||
25 | Since `require()` does a synchronous load, it can cause performance problems when used in other locations. | |
26 | ||
27 | Further, ES6 modules mandate that `import` and `export` statements can only occur in the top level of the module's body. | |
28 | ||
29 | ## Rule Details | |
30 | ||
31 | This rule requires all calls to `require()` to be at the top level of the module, similar to ES6 `import` and `export` statements, which also can occur only at the top level. | |
32 | ||
33 | Examples of **incorrect** code for this rule: | |
34 | ||
8f9d1d4d DC |
35 | ::: incorrect |
36 | ||
eb39fafa DC |
37 | ```js |
38 | /*eslint global-require: "error"*/ | |
39 | /*eslint-env es6*/ | |
40 | ||
41 | // calling require() inside of a function is not allowed | |
42 | function readFile(filename, callback) { | |
8f9d1d4d DC |
43 | var fs = require("fs"); |
44 | fs.readFile(filename, callback); | |
eb39fafa DC |
45 | } |
46 | ||
47 | // conditional requires like this are also not allowed | |
8f9d1d4d DC |
48 | if (DEBUG) { |
49 | require("debug"); | |
50 | } | |
eb39fafa DC |
51 | |
52 | // a require() in a switch statement is also flagged | |
8f9d1d4d DC |
53 | switch (x) { |
54 | case "1": | |
55 | require("1"); | |
56 | break; | |
57 | } | |
eb39fafa DC |
58 | |
59 | // you may not require() inside an arrow function body | |
60 | var getModule = (name) => require(name); | |
61 | ||
62 | // you may not require() inside of a function body as well | |
8f9d1d4d DC |
63 | function getModule(name) { |
64 | return require(name); | |
65 | } | |
eb39fafa DC |
66 | |
67 | // you may not require() inside of a try/catch block | |
68 | try { | |
69 | require(unsafeModule); | |
8f9d1d4d | 70 | } catch (e) { |
eb39fafa DC |
71 | console.log(e); |
72 | } | |
73 | ``` | |
74 | ||
8f9d1d4d DC |
75 | ::: |
76 | ||
eb39fafa DC |
77 | Examples of **correct** code for this rule: |
78 | ||
8f9d1d4d DC |
79 | ::: correct |
80 | ||
eb39fafa DC |
81 | ```js |
82 | /*eslint global-require: "error"*/ | |
83 | ||
84 | // all these variations of require() are ok | |
8f9d1d4d DC |
85 | require("x"); |
86 | var y = require("y"); | |
eb39fafa | 87 | var z; |
8f9d1d4d | 88 | z = require("z").initialize(); |
eb39fafa DC |
89 | |
90 | // requiring a module and using it in a function is ok | |
8f9d1d4d | 91 | var fs = require("fs"); |
eb39fafa | 92 | function readFile(filename, callback) { |
8f9d1d4d | 93 | fs.readFile(filename, callback); |
eb39fafa DC |
94 | } |
95 | ||
96 | // you can use a ternary to determine which module to require | |
8f9d1d4d | 97 | var logger = DEBUG ? require("dev-logger") : require("logger"); |
eb39fafa DC |
98 | |
99 | // if you want you can require() at the end of your module | |
100 | function doSomethingA() {} | |
101 | function doSomethingB() {} | |
102 | var x = require("x"), | |
103 | z = require("z"); | |
104 | ``` | |
105 | ||
8f9d1d4d DC |
106 | ::: |
107 | ||
eb39fafa DC |
108 | ## When Not To Use It |
109 | ||
110 | If you have a module that must be initialized with information that comes from the file-system or if a module is only used in very rare situations and will cause significant overhead to load it may make sense to disable the rule. If you need to `require()` an optional dependency inside of a `try`/`catch`, you can disable this rule for just that dependency using the `// eslint-disable-line global-require` comment. |