JShint and NodeJS in Sublime Text 3

JShint is a great tool for enforcing a specific coding style, but you might run into errors like the ones listed below when you first install JShint in Sublime Text 3.

errors 'require' is not defined
errors 'exports' is not defined
errors 'process' is not defined

The problem here is that the linter doesn’t know anything about the global variables that NodeJS exposes, and there for i reports an error, but there are a few ways to tell the linter about the global variables.

  1. Add /*global require, module, __dirname */ to the top you your .js files. This will tell jshint that these variables are global variables, and hence treat them as defined global variables.

  2. Add /*jslint node: true */ to the top of your .js files, this will tell the linter that it’s validating a NodeJS file and therefor also ignore the global variables

These options have to go into every single JS file that you have in your project, and this can be annoying if you have a big project, and if you’re like me and and you don’t like superfluous code, then this last approach is for you.

  1. Add a new file to the root of your project called .jshintrc and add this line {"node": true}. Doing so will tell jshint to treat all .js files like NodeJS files.