Vue-cli: vue cli eslint no-console error when no prod mode

Created on 18 Aug 2018  路  19Comments  路  Source: vuejs/vue-cli

Version

3.0.0-rc.3

Reproduction link

https://github.com/acgotaku/vue-cli-issue

Node and OS info

yarn 1.9.4 node 8.11.3 Archlinux

Steps to reproduce

First , clone repo and run yarn serve command to start project.
after, uncomment https://github.com/acgotaku/vue-cli-issue/blob/master/src/components/HelloWorld.vue#L35
console.log(msg) the terminal will output error: Unexpected console statement (no-console)

But if you stop yarn serve command and run again, the eslint error will disappear.

What is expected?

Only production mode output on console eslint error.

What is actually happening?

Add console.log to file will trigger eslint error.
But if you rerun the command,the eslint error will disappear.

eslint upstream

Most helpful comment

Make this modification to your package.json file, under "eslintConfig":

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

You need to restart "npm run serve" in order for it to honor your new change.

All 19 comments

The error is shown because it is in the eslint:recommended ruleset & you imported it in your ESLint configuration
https://github.com/eslint/eslint/blob/a857cd914200c56203a40a42dfbd69d9fe8dc351/conf/eslint-recommended.js#L97

If you want to distinguish production env for ESLint rules, please choose "placing config in dedicated config files" as such rule is only possible in a .js config file:

'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',

@acgotaku It's makeJSOnlyValue & your ESLint config lies in package.json 馃槄

@sodatea Thanks. But can you tell why rerun command the error disappear? If the rules form eslint:recommended, the error will output every times.

That's weird.

I believe it's a bug in vue-loader used in combination with cache-loader.
Will open an issue in the upstream library once I figure out a minimum reproduction.

I have the same problem, (vue cli 3.0.4)
error: Unexpected console statement (no-console)
but, I did nothing, restart dev server, the error was removed.
sometimes, the error will output.

eslint config put in package.json is not good way.
I don't like it.
json file can't run js,
can't write comments.

package.json is just meta data of the package.
do not put eslint config, postcss config...

Is this still unresolved. I have this issue and can't find a good workaround. Tried to set lintOnSave: 'error' in vue.config.js and the following in the eslintrc.js (and I tried the same thing with JSON format in eslintrc):

module.exports = {
  "extends": "eslint:recommended",
  "rules": {
    "no-console": "error",
  }
}

I thought this might force vue to inspect it. But inspection only happens on files that I actually change since the last npm run serve. So if it is cache related, is there a way (manually or in config) to clear the cache so that a npm run serve triggers recompilation and eslint checking of all files?

Make this modification to your package.json file, under "eslintConfig":

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

You need to restart "npm run serve" in order for it to honor your new change.

Lazy quickfix for those who don't want to tamper with anything: just use window.console.log

Emphasizing @mzoe1330 last line...
"_You need to restart "npm run serve" in order for it to honor your new change._"

Brilliant idea @mzoe1330 , I made your recommendation and it worked. Thanks.

eslint recommends that we should ship without console statement.
What is the correct way to do this
I mean, in production I want to strip off console

Lazy quickfix for those who don't want to tamper with anything: just use window.console.log

Works perfectly

vue.config.js

const TerserPlugin = require('terser-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
module.exports = {
  configureWebpack: {
    optimization: {
      minimize: true,
      minimizer: isProd ? [
        new TerserPlugin({
          terserOptions: {
            ecma: 6,
            compress: { drop_console: true },
            output: { comments: false, beautify: false }
          }
        })
      ] : []
    }
  }
}

// eslint-disable-next-line no-console
console.log()

vue.config.js

const TerserPlugin = require('terser-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
module.exports = {
  configureWebpack: {
    optimization: {
      minimize: true,
      minimizer: isProd ? [
        new TerserPlugin({
          terserOptions: {
            ecma: 6,
            compress: { drop_console: true },
            output: { comments: false, beautify: false }
          }
        })
      ] : []
    }
  }
}

Wonder why no one likes this one, as it's by far the best solution. All others are just ugly quickfixes.

Still having a problem with this!

The bug I'm seeing is that I get the "no-console" warnings the FIRST time I do yarn build and then the 2nd time (no restart, no change, just entering yarn build again) it works fine, no warnings. Odd!

Any ideas? Here's my files--

.eslintrc.js

module.exports = {
    root: true,
    env: {
        node: true
    },
    extends: [
        'plugin:vue/essential',
        'eslint:recommended'
    ],
    parser: 'vue-eslint-parser',
    parserOptions: {
        parser: 'babel-eslint'
    },
    rules: {
        'no-console': process.env.VUE_APP_ENV === 'production' ? 'error' : 'off',
        'no-debugger': process.env.VUE_APP_ENV === 'production' ? 'error' : 'off'
    },
    overrides: [
        {
            files: [
                '**/__tests__/*.{j,t}s?(x)',
                '**/tests/unit/**/*.spec.{j,t}s?(x)'
            ],
            env: {
                jest: true
            }
        }
    ]
}

package.json

    ...
    ...
    ...
    "eslintConfig": {
        "rules": {
            "no-console": "off"
        }
    }

@sodatea But in here https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-eslint/eslintOptions.js#L7
cli-plugin-eslint set the rule, why no effect?

@acgotaku can you edit your comment to provide a permalink? the link you provided is referencing HEAD which has evidently changed since you commented.

@sodatea But in here https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-eslint/eslintOptions.js#L7
cli-plugin-eslint set the rule, why no effect?

@acgotaku can you edit your comment to provide a permalink? the link you provided is referencing HEAD which has evidently changed since you commented.

Updated~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JIANGYUJING1995 picture JIANGYUJING1995  路  3Comments

NathanKleekamp picture NathanKleekamp  路  3Comments

brandon93s picture brandon93s  路  3Comments

BusyHe picture BusyHe  路  3Comments

Gonzalo2683 picture Gonzalo2683  路  3Comments