]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-new-require.md
f168795f3c53c10e3855a207f2ac863abe881bde
[pve-eslint.git] / eslint / docs / src / rules / no-new-require.md
1 ---
2 title: no-new-require
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
9
10 The `require` function is used to include modules that exist in separate files, such as:
11
12 ```js
13 var appHeader = require('app-header');
14 ```
15
16 Some modules return a constructor which can potentially lead to code such as:
17
18 ```js
19 var appHeader = new require('app-header');
20 ```
21
22 Unfortunately, this introduces a high potential for confusion since the code author likely meant to write:
23
24 ```js
25 var appHeader = new (require('app-header'));
26 ```
27
28 For this reason, it is usually best to disallow this particular expression.
29
30 ## Rule Details
31
32 This rule aims to eliminate use of the `new require` expression.
33
34 Examples of **incorrect** code for this rule:
35
36 ::: incorrect
37
38 ```js
39 /*eslint no-new-require: "error"*/
40
41 var appHeader = new require('app-header');
42 ```
43
44 :::
45
46 Examples of **correct** code for this rule:
47
48 ::: correct
49
50 ```js
51 /*eslint no-new-require: "error"*/
52
53 var AppHeader = require('app-header');
54 var appHeader = new AppHeader();
55 ```
56
57 :::
58
59 ## When Not To Use It
60
61 If you are using a custom implementation of `require` and your code will never be used in projects where a standard `require` (CommonJS, Node.js, AMD) is expected, you can safely turn this rule off.