]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-console.md
05a1dfef51a4545727da481b5984122a1dc7ac11
[pve-eslint.git] / eslint / docs / src / rules / no-console.md
1 ---
2 title: no-console
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-alert
7 - no-debugger
8 ---
9
10
11 In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on `console`. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using `console` should be stripped before being pushed to production.
12
13 ```js
14 console.log("Made it here.");
15 console.error("That shouldn't have happened.");
16 ```
17
18 ## Rule Details
19
20 This rule disallows calls or assignments to methods of the `console` object.
21
22 Examples of **incorrect** code for this rule:
23
24 ::: incorrect
25
26 ```js
27 /* eslint no-console: "error" */
28
29 console.log("Log a debug level message.");
30 console.warn("Log a warn level message.");
31 console.error("Log an error level message.");
32 console.log = foo();
33 ```
34
35 :::
36
37 Examples of **correct** code for this rule:
38
39 ::: correct
40
41 ```js
42 /* eslint no-console: "error" */
43
44 // custom console
45 Console.log("Hello world!");
46 ```
47
48 :::
49
50 ## Options
51
52 This rule has an object option for exceptions:
53
54 * `"allow"` has an array of strings which are allowed methods of the `console` object
55
56 Examples of additional **correct** code for this rule with a sample `{ "allow": ["warn", "error"] }` option:
57
58 ::: correct
59
60 ```js
61 /* eslint no-console: ["error", { allow: ["warn", "error"] }] */
62
63 console.warn("Log a warn level message.");
64 console.error("Log an error level message.");
65 ```
66
67 :::
68
69 ## When Not To Use It
70
71 If you're using Node.js, however, `console` is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
72
73 Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
74
75 ```js
76 /* eslint no-console: ["error", { allow: ["warn"] }] */
77 console.error = function (message) {
78 throw new Error(message);
79 };
80 ```
81
82 With the `no-console` rule in the above example, ESLint will report an error. For the above example, you can disable the rule:
83
84 ```js
85 // eslint-disable-next-line no-console
86 console.error = function (message) {
87 throw new Error(message);
88 };
89
90 // or
91
92 console.error = function (message) { // eslint-disable-line no-console
93 throw new Error(message);
94 };
95 ```
96
97 However, you might not want to manually add `eslint-disable-next-line` or `eslint-disable-line`. You can achieve the effect of only receiving errors for console calls with the `no-restricted-syntax` rule:
98
99 ```json
100 {
101 "rules": {
102 "no-console": "off",
103 "no-restricted-syntax": [
104 "error",
105 {
106 "selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
107 "message": "Unexpected property on console object was called"
108 }
109 ]
110 }
111 }
112 ```