Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint dollar($) is not defined. (no-undef)

Tags:

jquery

eslint

People also ask

How do I fix no undef?

The React. js error "X is not defined react/jsx-no-undef" occurs when we forget to import a function, class or a variable in our code before using it. To solve the error, make sure to import the value before using it in your code, e.g. import {myFunc} from 'my-package' . Copied!

How do I disable no undef ESLint?

You can disable ESLint for a given line using a // eslint-disable-line comment. For example, the below code would cause ESLint to complain because of the no-use-before-define rule if you remove the eslint-disable-line comment. A eslint-disable-line comment disables all ESLint rules for a given line.


You are missing

"env": {
  "browser": true,
  "commonjs": true,
  "es6": true,
  "jquery": true
},

$ is not declared as a global without jquery environment enabled. Because of that, you are getting a no-undef error, saying that you are using variable that haven't been declared.


https://eslint.org/docs/user-guide/configuring#specifying-environments

You can specify environments using a comment inside of your JavaScript file, use the following format:

Add the line below as a comment at the beginning of your JavaScript file.

    /* eslint-env jquery */

The eslinter will stop throwing undefined on '$' because it will know you are working with jQuery.


You can also add this line to the top of your js file:

/* global $ */

To prevent warning over "$", or for any other global like "varName":

/* global varName */

In .eslintrc.js

Add

{
  "globals": {
    "$": true
  }
}

See https://eslint.org/docs/user-guide/configuring#specifying-globals