Many grunt.js-script starts with:
/*global module:false*/
module.exports = function(grunt) {
But what the cause of the comment in the first line?
Global modules are node packages that are installed on your system rather than your project directory. They allow us to use the package as a tool anywhere on the local computer. By saying global, we are talking about the scope of usage of these modules.
To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.
The globalThis property provides a standard way of accessing the global this value (and hence the global object itself) across environments. Unlike similar properties such as window and self , it's guaranteed to work in window and non-window contexts.
While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module. You can read more about it in the NodeJS documentation where it says: global.
It's a directive for JSLint or JSHint. It tells the JSLint/JSHint parser that the identifier module
is defined elsewhere, so it doesn't throw an error telling you that module
is undefined. Without it, the parser will encounter the reference to module
and think that you're trying to refer to an undefined variable.
From the JSLint docs:
JSLint also recognizes a
/*global*/
directive that can indicate to JSLint that variables used in this file were defined in other files. The directive can contain a comma separated list of names.
And the JSHint docs:
In addition to options, you can let JSHint know what global variables it should expect:
/*global DISQUS:true, jQuery:false */
In the example above, JSHint will allow you to override
DISQUS
, but complain if you try to overridejQuery
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With