-h1h1w
Last Updated: October 17, 2016
·
44.03K
· calamari
6b4b7f11163b1e87571995ce8c3f0bce

How to use jslint in node.js projects properly

Javascript is a highly dynamic language and there is no doubt about having a control mechanism like JSLint that prevents you from writing really stupid code in the first place.

But when writing node.js code, you probably get a lot of false positive problems counted, if you JSLint your code. Most probably they are all about require is not defined or module is nor defined. To declare those as ok it might be tempting to just add those objects which are defined by node.js to a directive at the top of your node.js program file. Something like:

/*global require, module,  __dirname */

But there is an easier, and more semantical way to do just that. Simply put the following JSLint option

/*jslint node: true */

at the top of your files instead, and all magical objects that can be used by node.js will no longer be marked as problems. Plus, if you do it like this, it is saying to the jslinter that it handles a node.js program, so if you also (and you should) put in a "use strict"; statement, it will not be marked as something that has to be put in a function.

So this tend to be a good starting point for every node.js file:

/*jslint node: true */
"use strict";
Say Thanks
Respond

7 Responses
Add your response

1653
Chakaroff

I've found that the best way to lint .js files is directly in Sublime Text 2.

over 1 year ago ·
1654
6b4b7f11163b1e87571995ce8c3f0bce

Yes, and there it is essential, that you only see problems, that really are problems, else you are either distracted due to many false problems or you simply start ignoring them. It's like failing tests.

over 1 year ago ·
10572
425e8f19c8bd0816024d527097a39880

If you're using Intelliji WebStorm, you can set this option in JSHint options, section called "Environmnent"

over 1 year ago ·
14266
D62647c8ff2c1b9cc83f6569843fdc44

There shouldn't be a space after the first asterisk:
/*jslint node: true */

over 1 year ago ·
14267
6b4b7f11163b1e87571995ce8c3f0bce

Yes, you're right. There was a typo in the second jslint comment. I fixed it.

Thanks,

over 1 year ago ·
14498
64269f993910e70079dc9d02b127e5d5

very helpful, thanks

over 1 year ago ·
28250

Great tip, can even be automated by Grunt!:

BEFORE
javascript jshint: { ignore_warning: { options: { '-W117': true, // ignores ‘require is not defined’ error '-W097': true, // allows non-function form of ‘use strict’ }, src: [‘src/**/*.js’] }, },

AFTER*
javascript jshint: { src: ['src/**/*.js'], options: { node: true } // enables error-free use of 'require' and 'use strict' keywords },

**This implementation is especially great for larger codebases that need to be auto-linted just before being pushed into production.

Thanks!

10 months ago ·
Awesome Job

15d1aac2 6d9b 11e7 919f 0256eab75a5e
Support Engineer
·
Europe or Eastern US
·
Full Time