Error "is not defined" - how to use several client-side JS files?


#1

I have a project with files

  • js.1.js
  • js2.js

In the js1.js file I create a new function. In j2.js I make a reference to the function created in js1.js. Unfortunately, Gomix does not recognize that the JS files should be aware of each other and displays an error message in the editor “‘Car’ is not defined.” Both files are in the index.html and running the code in a browser works.

Is it possible to configure Gomix editor so that the JS files are aware of each other? Or is this a bug?

Link to project: https://gomix.com/#!/project/soft-argument


#2

This is not a Gomix error so much as a JSHint error (jshint is the thing that gomix uses to check your Javascript for simple errors).

While you can run your site as is without an issue, if you want to the error to go away you need to tell JSHint that you have defined the global variable “Car” somewhere else.

Add this comment to the top of js2.js:

/* globals Car */

See the jshint docs for more http://jshint.com/docs/


#3

Hi simon,

RedRiderX is correct, and that command will fix your issue :slight_smile: To be more precise, we use eslint (http://eslint.org/) which is similar to jshint but has better support for the latest JavaScript versions :slight_smile:


#4

Oh that’s good to know. I only really assumed JSHint because that’s what I was familiar with.

On a side note, does that mean I could add “.eslintrc” files to my project and Gomix would use them for linting settings?


#5

Not yet, but it’s in the to-do list :slight_smile:


#6

Looking forward to that feature


#7

+1 to eslintrc support in the editor. Is there somewhere we can go to actually vote on feature requests?


#8

I declare my globals at the top of my files with the following syntax:

/*global describe beforeAll it expect*/

The globals in this case are for Jest tests: describe, beforeAll, it, and expect.

This sort of declaration currently resolves the ESLint errors.