Enable console.log and alert statements in LWC


Hello,

This post is about a very small and somewhat confusing error that you come across when you are doing LWC development. 

Unexpected console statement. eslint(no-console)
Unexpected console statement. eslint(no-alert)

Why does this error occur?
-      This is because ESLint(Linter tool which analyzes code and throws error, when it finds programmatical bugs, syntactical errors, etc.,) considers using console statements or alert statement as a bad practice
-      It is considered as bad practice as console and alert statements are used for debugging purpose and they should not be shipped to the client.

What is the solution?
To overcome this there are 2 ways:
1.   You can disable ESLint’s rule for the whole JS file
export default class accountComponent extends LightningElement {
/* eslint-disable no-console */
/* eslint-disable no-alert */

handleClick(event){
console.log('Event : '+event.target.label);
}
}
2.   You can disable ESLint’s rule for one specific line
export default class accountComponent extends LightningElement {
handleClick(event){
        // eslint-disable-next-line no-console
        console.log('Event : '+event.target.label);
    }
}


This will get rid of the errors. But please be cautious and remove all your console and alert statements before you push your code to the client.

Resources:



Comments

Popular posts from this blog

SFDX Push Error : An object 'ComponentName' of type LightningComponentBundle was named in package.xml, but was not found in zipped directory

Salesforce Apex Triggers Interview Questions/Scenarios