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.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x