55

I'm writing some code for Node.js and I'm currently using JSHint to check over my code. However, when I use the require function to import modules, it says:

'require' is not defined.

How can I suppress the warning?

"use strict";
var express = require('express');   
var register = require('./routes/register');
hong4rc
  • 3,334
  • 2
  • 16
  • 36
somesh
  • 579
  • 1
  • 5
  • 11
  • 1
    try putting /*global require*/ to the file that you don't want to see, this worked for me – cubbuk Dec 12 '14 at 07:28

4 Answers4

87

jshint is not aware of node.js globals by default you need to inform it.

add this comment to the top:

/* jshint node: true */

generalhenry
  • 16,677
  • 4
  • 43
  • 59
  • 1
    I have already added that comment but still i have that issue. – somesh Apr 09 '13 at 05:54
  • can you post your settings comment – generalhenry Apr 09 '13 at 06:01
  • /*jshint node: true, bitwise:true, curly:true, forin:true, noarg:true, noempty:true, nonew:true, undef:true, strict:true, browser:true, node:true, asi:false , vars:true, es5: true, evil: true, nomen: true */ – somesh Apr 09 '13 at 06:05
  • I check it with jshint.com, it complained about `vars` as an options, try `/*jshint node: true, bitwise:true, curly:true, forin:true, noarg:true, noempty:true, nonew:true, undef:true, strict:true, browser:true, node:true, asi:false, es5: true, evil: true, nomen: true */` – generalhenry Apr 09 '13 at 06:09
  • Just to clarify, the comment should be added to the top of the *.js* file. – jlukanta Jul 06 '16 at 19:00
59

We can set node as the global environment variable in JSHint's .jshintrc file

This option defines globals available when your code is running inside of the Node runtime environment. Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model. This option also skips some warnings that make sense in the browser environments but don't make sense in Node such as file-level use strict pragmas and console.log statements.

For more info http://jshint.com/docs/options/#node

{
   "node": true
}

Errors like 'require' is not defined, 'console' is not defined, 'module' is not defined won't show up any more

Sridhar
  • 9,355
  • 5
  • 37
  • 40
7

You can configure JSHint by adding "require" to the .jshintrc file. For instance:

{
    "globals" : {
        "require": false
    }
}

Or you can define globals per specific file only by:

/* global require */

For more information about how to configure JSHint please read JSHint Documentation

Vlad Bezden
  • 59,971
  • 18
  • 206
  • 157
2

I stumbled upon this answer and couldn't get anything to work in VSCode. I got this to work:

  1. Open JSON Settings file using the Preferences: Open Settings (JSON) via the Command Palette Window (Ctrl-Shift-P).
  2. Add this section into the settings.json file:

    { "jshint.options": { "esversion": 8, // This gets rid of some other warnings "node": true } }

jaredbaszler
  • 2,579
  • 2
  • 23
  • 32