86

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.

Emiliano Zilocchi
  • 1,075
  • 1
  • 8
  • 12

3 Answers3

87

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

{
  "globals": { "MyProject": true }
}
esamatti
  • 18,293
  • 11
  • 75
  • 82
63

This is only for globals

/* global MyProject */

In your case you need

/* exported MyProject */
zevero
  • 2,312
  • 21
  • 12
  • 1
    this is a good answer if you're working with one JavaScript file, thanks! –  Aug 18 '18 at 22:32
4

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/

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • It doesn't sound like they're using the web interface - `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.` and `Some kind on option in the config/jshint.yml would be nice` – Ian Jul 17 '13 at 20:46
  • 1
    I've just found it, the 'predef' attribute under config/jshint.yml. it's a comma separated attribute. Thanks! – Emiliano Zilocchi Jul 17 '13 at 20:47
  • 1
    Worth mentioning `predef` [exists in JSLint too](https://github.com/douglascrockford/JSLint/blob/master/jslint.js#L37). – ruffin Jul 18 '13 at 01:24
  • Given Visual Studio 2013 with Web Essentials and a conventional hierarchy of solution folder and project sub-folders, you can put it in the solution root folder. – George Jan 18 '14 at 15:24
  • Undeserved downvotes. The question did not make it clear that Rails was being used until after this answer was left. +1 – superluminary Mar 04 '14 at 16:32