RoR weird application.js warnings

rails
javascript
ruby

#1

I haven’t changed anything in the application.js file, but all of a sudden it got filled with warnings saying $ function is not defined. But I do request //= require jquery and have gem ‘jquery-rails’, ‘4.0.3’ installed. My app works just fine but all the warnings are really annoying

Any ideas ?


$ being flagged as not defined in js file when using jQuery
#2

Hi @Cat_Lin,

those warnings are coming from a lint tool. This tool is helping you to find potential mistakes by analysing your JavaScript code. And while the tool can seem naggy or annoying, it has only good intentions. :slight_smile:

In your case it is telling you that you’re using a variable without defining it. This could be a potential bug. The lint tool doesn't know that you're importing jQuery and that jQuery makes a global variable called available.

The tool is telling you how to fix this warning. Either there’s a bug in your code (using a variable that doesn’t exist) or you’re using a global variable (what you’re doing). Solution is to put the comment from the tooltip at the top of your file: /* global $ */

While it seems annoying, consider that such tools are widely used across the industry to check for potential bugs. They are very helpful to increase code quality and are incorporate in many build processes.
It is also helpful for others who work with your code. They immediately see at the top of the file what global variables you assume to be available when you run the code.

If you want to learn more about JavaScript lint tools, google for JSLint, JSHint or ESLint (the most common lint tools). c9.io is using ESLint.

Cheers!


#3

Hello b2m9,

thank you for everything, I do understand the necessity and help these kind of tools provide, the thing was the wave of warnings appeared all at once without me having an idea why. Probably I have deleted such a comment as /* global $ */ without noticing it…

Cheers!


#4

I put the /* global $ */ and still am getting the error. it has went away on the page where the js was written but not in the console warnings when you inspect the page and the script isnt running