55

Possible Duplicate:
JSLint: was used before it was defined

I run JSlint and saw errors like that:

'foo' is not defined.
var x = foo();

foo is a function defined in another JavaScript file foo.js. As I understand there is no "import / require" directives in JavaScript to reference the foo function defined in another source file.

How can I fix this error repoted by JSlint?

Community
  • 1
  • 1
Michael
  • 10,185
  • 12
  • 59
  • 110
  • 3
    possible duplicate of [JSLint: was used before it was defined](http://stackoverflow.com/questions/9621162/jslint-was-used-before-it-was-defined) and [“`[Variable]` was used before it was defined” error](http://stackoverflow.com/questions/8134049/variable-was-used-before-it-was-defined-error). – Felix Kling Aug 14 '12 at 17:58

1 Answers1

88

Use the global directive to tell JSLint about foo's assumed existence.

/*global foo */

http://www.jslint.com/help.html#global

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 4
    I alway's thought it was without space: `/*global` – GitaarLAB Aug 14 '12 at 17:58
  • 2
    You're right; I wasn't sure it actually made a difference. Then I tested, and `/*global` works but `/* global` doesn't. – Matt Ball Aug 14 '12 at 18:00
  • you just beat me to it by a second haha, figured to add this as a comment since the question is indeed a duplicate – GitaarLAB Aug 14 '12 at 18:01
  • 2
    This is good but I don't like to litter my production code with test or code quality snippets. I think it would be better to declare these in a separate file confined to the test environment. – B Seven May 05 '15 at 16:48
  • @BSeven since it's a comment, your JS minification tooling should remove it automatically for production, right? `:)` – Matt Ball May 05 '15 at 18:22
  • 2
    My concern is not about download speed. It's pollution of the source that is read by developers. – B Seven May 05 '15 at 21:15
  • I see. When you said "production code" I thought you meant "code that users receive." – Matt Ball May 05 '15 at 21:21
  • I manually added `socket.io` into my client html, and js (angular controller) and got "io not defined", while running `gulp build`. This method works well. – Zhe Hu Oct 23 '15 at 17:12
  • 8
    There also is a `.jshintrc` file that has a `globals` section where you can define those. That way you don't have to pollute your source files with things like that. – lyio Feb 09 '16 at 12:21