]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-new-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 | The `require` function is used to include modules that exist in separate files, such as: |
10 | ||
11 | ```js | |
12 | var appHeader = require('app-header'); | |
13 | ``` | |
14 | ||
15 | Some modules return a constructor which can potentially lead to code such as: | |
16 | ||
17 | ```js | |
18 | var appHeader = new require('app-header'); | |
19 | ``` | |
20 | ||
21 | Unfortunately, this introduces a high potential for confusion since the code author likely meant to write: | |
22 | ||
23 | ```js | |
24 | var appHeader = new (require('app-header')); | |
25 | ``` | |
26 | ||
27 | For this reason, it is usually best to disallow this particular expression. | |
28 | ||
29 | ## Rule Details | |
30 | ||
31 | This rule aims to eliminate use of the `new require` expression. | |
32 | ||
33 | Examples of **incorrect** code for this rule: | |
34 | ||
8f9d1d4d DC |
35 | ::: incorrect |
36 | ||
eb39fafa DC |
37 | ```js |
38 | /*eslint no-new-require: "error"*/ | |
39 | ||
40 | var appHeader = new require('app-header'); | |
41 | ``` | |
42 | ||
8f9d1d4d DC |
43 | ::: |
44 | ||
eb39fafa DC |
45 | Examples of **correct** code for this rule: |
46 | ||
8f9d1d4d DC |
47 | ::: correct |
48 | ||
eb39fafa DC |
49 | ```js |
50 | /*eslint no-new-require: "error"*/ | |
51 | ||
52 | var AppHeader = require('app-header'); | |
53 | var appHeader = new AppHeader(); | |
54 | ``` | |
55 | ||
8f9d1d4d DC |
56 | ::: |
57 | ||
eb39fafa DC |
58 | ## When Not To Use It |
59 | ||
60 | 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. |