How to tell JSLint / JSHint what global variables are already defined

JavascriptGlobal VariablesJslintJshint

Javascript Problem Overview


In my project we have some global variables that work as containers:

MyProject.MyFreature.someFunction = function() { ... }

So then I use that script across the site and JSLint / JSHint complains about that:

> 'MyProject' is not defined

I know that I can go to every JavaScript file and add the comment /*global MyProject*/ on top of it. But I'm looking a way to define that comment in some sort of config file so I don't have to go file by file adding this comment.

Some kind on option in the config/jshint.yml would be nice.

Javascript Solutions


Solution 1 - Javascript

For JSHint you can create .jshintrc to your project directory with

{
  "globals": { "MyProject": true }
}

Solution 2 - Javascript

This is only for globals

/* global MyProject */

In your case you need

/* exported MyProject */

Solution 3 - Javascript

JSLint has a textarea below the options that says predefine global variables here in it. Just add the variable names in there before running the check.

JSHint doesn't allow you to add global variables, but you can uncheck the When variable is undefined option to suppress that warning.

The JSHint library also has parameters for globals, if you run it as a library . . . details in here: http://jshint.com/docs/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmiliano ZilocchiView Question on Stackoverflow
Solution 1 - JavascriptEpeliView Answer on Stackoverflow
Solution 2 - JavascriptzeveroView Answer on Stackoverflow
Solution 3 - JavascripttalemynView Answer on Stackoverflow