error '_' is not defined no-undef - lodash

I use eslint to check my code ,and a error happen -- "error '_' is not defined no-undef".
I write the code like this:
new webpack.ProvidePlugin({
$: "jquery",//jquery
jQuery: "jquery",
"window.jQuery": "jquery",
_:"lodash"//lodash
})
and get .eslintrc.js, part of it like this:
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"jquery":true,
},
before I add the jquery into ,it happen an error -- "error '_' is not defined no-undef".
However, when I add lodash into ,just like that "'lodash':true".It doesn't work.
Can you help me?

ESLint doesn't understand lodash library.
you need to add a global section in your config.
"globals": {
"_": true
},

Related

Is there a way to fix this error in prettier, in nuxt / vue environment

I just ran NPM update on a project that was working fine. Now, I am getting a Prettier "Friendly Error". I'm wondering if ESLint and Prettier are not playing well together in my config.
error Replace `⏎··················Coming·Soon!⏎················` with `Coming·Soon!`
I'm not really sure what is going on here, but it looks like it's a formatting issue telling me to add backticks. The errors are on HTML markup that does not even have qoutes on it. It's literally <span>Coming Soon</span>.
.eslintrc.js:
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
parserOptions: {
parser: 'babel-eslint',
},
extends: [
'#nuxtjs',
'prettier',
'prettier/vue',
'plugin:prettier/recommended',
'plugin:nuxt/recommended',
],
plugins: ['prettier'],
rules: {},
}
.prettierrc:
{
"semi": false,
"singleQuote": true,
"htmlWhitespaceSensitivity": "ignore"
}
The error isn't indicating the backticks. It's telling you the whitespace around Coming Soon! should be removed.
The config for htmlWhitespaceSensitivity can be confusing:
ignore - HTML whitespace is insignificant, so remove it
strict - HTML whitespace is significant, so ignore it
Thus you actually want to use strict. Configure ESLint as shown below (and restart IDE if using VS Code):
// .eslintrc.js
module.exports = {
rules: {
'prettier/prettier': {
htmlWhitespaceSensitivity: 'strict',
},
},
}
Note that htmlWhitespaceSensitivity config doesn't seem to have an effect in .prettierrc.

Why I can't use debugger or console.log on my Vue app

I just created a new Vue app through Vue CLI but I can't use either debugger or console.log otherwise I get an error in the browser, why and how can I allow it ?
Unexpected 'debugger' statement (no-debugger) at src/components/SomeComponent.vue:48:7
In my case it was because I went with the default configs when creating my project and it includes eslint:
So in order to allow debugger and console.log statements I modified the rules on my package.json file like this:
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {
"no-console": 1,
"no-debugger": 1
},
"parserOptions": {
"parser": "babel-eslint"
}
}
This way I still get a warning when compiling so I don't forget to remove them before committing but I can run my app and use those statements.
You can use:
//eslint-disable-next-line no-console
only if your really have to use console.log()
otherwise I highly recommend to use a logger like 'vuejs-logger'.
What happens is like in production you still have these console.log lines that I actually don't like a lot... plus the warning during rebuilding prevents you from using the hot reload of your app during development.

How to set vscode format Vue template automatically on save?

I've modified the settings.json file, but it doesn't work.
Here it is:
{
"eslint.autoFixOnSave": true,
"vetur.format.defaultFormatter.html":"js-beautify-html"
}
In your settings.json you should have:
editor.formatOnSave: true
[vue]: {"editor.defaultFormatter": "octref.vetur"} if you have several formatters registered for .vue files you need to specify which one to use (otherwise format on save will not know which one to use and it will default to do nothing. This will select "Vetur" as the default.
"vetur.format.defaultFormatter.html": "js-beautify-html" to tell Vetur how to format the part inside <template> tags:
{
"editor.formatOnSave": true,
"vetur.format.defaultFormatter.html": "js-beautify-html",
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
}
}
Note: How do you know that there are several formatters registered for .vue? If when you use the Format Document action you get the dialog There are multiple formatters for 'Vue' files. Select a default formatter to continue then it means that you have more that one formatter registed for '.vue' files.
use plugin:vue/recommended replace plugin:vue/essential
// .eslintrc.js
module.exports = {
extends: [
'plugin:vue/recommended',
'#vue/standard'
]
}
enable eslint fix on save
// .vscode/settings.json
{
"eslint.validate": [
"javascript",
"html",
"vue"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.stylelint": true
},
"vetur.validation.template": false
}
To get the auto-save of templates to work using the combination of ESLint and Vetur, I used the following combination of VS Code settings:
{
"eslint.validate": [
"vue",
"html",
"javascript",
"typescript"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"vetur.format.defaultFormatterOptions": {
"prettier": {
"singleQuote": true
}
}
}
The "editor.formatOnSave": true did the trick for me. This lead to the problem of converting single quotes to double quotes in the script section, therefore I added the prettier singleQuote config as well.
My answer is just loosely coupled to the question but as this question is the top result for googling "vetur auto format not working" I would like to add a possible solution to that.
My template had the lang attribute set like this <template lang="">. After removing the attribute auto formatting started to work again.

Object doesn't support property or method 'replace' on Internet Explorer 11

I am trying to make my Vuejs application work on IE11. However, one node module (vue-directive-tooltip) throw an error on IE11:
Object doesn't support property or methode "replace"
The module is supposed to be IE11 compatible. I have tried to require the polyfill I need at the top of the entry point to my application. I have also tried to add the node module to the transpile dependencies.
vue.config.js:
require("#babel/polyfill");
configureWebpack: {
entry: ["#babel/polyfill", path.resolve(__dirname, "./src/main.js")],
}
babel.config.js:
module.exports = {
"presets": [
[
"#babel/preset-env",
{
"targets": {
"browsers": [ ">0.25%"]
},
"useBuiltIns": "entry",
"debug": true
}
]
],
"plugins": [
"#babel/plugin-proposal-object-rest-spread"
]
};
I expect the vue-directive-tooltip to work on IE11, but the actual output is the following error message:
SCRIPT438: Object doesn't support property or method 'replace'.
Adding the classList.js reference in the index.html was the solution.

stylus style formatting in vue files with VSCode

I have a vue file composed as followed (not really important) :
<template>
//some pseudo-html
</template>
<script>
//some javascript
</script>
<style lang='stylus'>
#import '~variables'
.card
cursor pointer
//some more stylus
</style>
I have formatOnSave activated in VSCode, I also have vetur and esLint installed.
When I use CSS code inside a classic <style> tag, I have no problem, but when I use lang='stylus', ESLint is still looking for CSS ( I get syntax errors like [css] { expected (css-lcurlyexpected) ).
Also, the auto-format on save just mess eveything up when I use stylus, it puts everything on the same line. Result after save :
<style lang='stylus'>
#import '~variables'
.card cursor pointer position relative padding //some more stylus
</style>
I tried to change the followinf settings in vscode :
vetur.format.defaultFormatter.css
vetur.format.defaultFormatter.stylus
but to no avail.
My current settings:
{
"workbench.colorTheme": "FlatUI Immersed",
"workbench.iconTheme": "material-icon-theme",
"files.trimTrailingWhitespace": true,
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"editor.detectIndentation": false,
"editor.formatOnPaste": false,
"editor.formatOnType": true,
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "all",
"editor.minimap.enabled": false,
"editor.mouseWheelScrollSensitivity": 2,
"editor.tabSize": 4,
"editor.fontSize": 15,
"window.zoomLevel": -1,
"workbench.startupEditor": "newUntitledFile",
"markdown.extension.preview.autoShowPreviewToSide": true,
"markdown.preview.breaks": true,
}
And workspace specific settings :
"settings": {
"files.associations": {
"*.vue": "html"
},
"editor.tabSize": 2,
"editor.formatOnSave": true,
// Defines space handling before function argument parentheses. Requires TypeScript >= 2.1.5.
"typescript.format.insertSpaceBeforeFunctionParenthesis": true,
// Defines space handling before function argument parentheses. Requires TypeScript >= 2.1.5.
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
// Eslint options
"eslint.enable": true,
"eslint.options": {
"extensions": [
".html",
".js",
".vue",
".jsx"
]
},
// An array of language ids which should be validated by ESLint
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "html",
"autoFix": true
}
],
// Run the linter on save (onSave) or on type (onType)
"eslint.run": "onSave",
// Turns auto fix on save on or off.
"eslint.autoFixOnSave": true,
}
It would be absolutely awesome if someone knows how to have the formatter and the linter correctly take that into account, but after 2 hours digging, i would honestly settle for a way to disable the formatter and the linter just for the <style> tag.
So, after digging some more, i found a solution :
delete this part of the settings :
"files.associations": {
"*.vue": "html"
},
And replace html by vue in this part :
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "vue",
"autoFix": true
}
],
And also add :
"vetur.format.defaultFormatter.js": "none"