How to disable the warning 'define' is not defined using JSHint and RequireJS

JavascriptRequirejsAmdJshint

Javascript Problem Overview


I uses RequireJS AMD in my project. When i run jshint on my project, it throws error like

In AMD Scripts

 'define' is not defined.

In Mocha test cases

 'describe' is not defined.
 'it' is not defined.

How to remove this warning in jshint?

Javascript Solutions


Solution 1 - Javascript

Just to expand a bit, here's a .jshintrc setup for Mocha:

{
  ....
  "globals"   : {
    /* MOCHA */
    "describe"   : false,
    "it"         : false,
    "before"     : false,
    "beforeEach" : false,
    "after"      : false,
    "afterEach"  : false
  }
}

From the JSHint Docs - the false (the default) means the variable is read-only.

If you are defining globals only for a specific file, you can do this:

/*global describe, it, before, beforeEach, after, afterEach */

Solution 2 - Javascript

jshint: {
  options: {
    mocha: true,
  }
}

is what you want

Solution 3 - Javascript

To avoid the not defined warning in jshint for the javascript add comments like:

/*global describe:true*/

Options

Solution 4 - Javascript

Add this in your .jshintrc

"predef" : ["define"]	// Custom globals for requirejs

Solution 5 - Javascript

late to the party, but use this option in your jshintrc:

"dojo": true

and thou shall rest peacefully without red warnings...

Solution 6 - Javascript

If you are working on node js. Add these two lines in the beginning of your file

/*jslint node: true */
"use strict";

Solution 7 - Javascript

Read the docs and search for /*global

Solution 8 - Javascript

If you're trying to run JSHint in WebStorm with Mocha, as I am, go into:

WebStorm > Preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSHint

Scroll down to "Environments" and make sure you have selected the checkbox to enable "Mocha" which will set up the definitions for JSHint for Mocha for you.

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
QuestionFizer KhanView Question on Stackoverflow
Solution 1 - JavascriptbendytreeView Answer on Stackoverflow
Solution 2 - JavascriptGrant FongView Answer on Stackoverflow
Solution 3 - JavascriptRoland PuntaierView Answer on Stackoverflow
Solution 4 - JavascriptShital ShahView Answer on Stackoverflow
Solution 5 - JavascriptGilad PelegView Answer on Stackoverflow
Solution 6 - JavascriptsalihcenapView Answer on Stackoverflow
Solution 7 - JavascriptPaul GrimeView Answer on Stackoverflow
Solution 8 - JavascriptnyarashaView Answer on Stackoverflow