Log to console in a Lightning Web Component

I am attempting to write a Lightning Web Component, but continually run into errors in my Javascript that I can’t debug.

I’ve been trying to log my error but when I put console.log() anywhere in the methods that are failing, I get the error:

Unexpected console statement.

Answer

You are most likely encountering ESLint errors in VS Code.

As an example below is how my code looks like with those warnings. However I am still able to save the component and utilize alert or console.log statements specifically for debugging purposes.

Your options are:

  1. Just ignore those warnings

  2. You can choose to suppress those warnings by right clicking on those to suppress either for that particular line or whole file. It though introduces following comments in your file.

    If you choose for whole file, the following is added in your file

    /* eslint-disable no-console */
    

    If you choose for specific line, the following is added before every such line

    // eslint-disable-next-line no-console
    
  3. By editing the .eslintrc.json available in your project by adding the below rules. This way you don’t have to address individual files. (My preferred way)

    "rules": {
        "no-console": "off",
        "no-alert": "off"
    }
    

Console showing errors

enter image description here

Attribution
Source : Link , Question Author : BlondeSwan , Answer Author : Jayant Das

Leave a Comment