Objective
Jshint is used to analyze and verify that JavaScript code conforms to your coding rules. This powerful tool can help you find bugs and problems in your code, and it forces your team to maintain certain coding conventions and styles that make the code reliable and easier to read.
In this article, I'll show you how to install, configure, and use Jshint. Also included is an example that lists some of my favorite editors that can use Jshint.
Install Jshint
Installing Jshint is very easy, and you can do it using the Node Package Manager (NPM). If you haven't installed NPM yet, you can download the latest version from NODEJS website to install Node.js and NPM.
Once NPM is installed, you can install jshint using the following command:
-g
parameter is npm
to tell us to install globally so that we can use this command in any directory.
Checking code by command line
Now that the Jshint is installed, let's use the command-line method jshint
to parse a JavaScript code file.
The following is a file named Demo1.json:
We use the following command to parse the code:
Jshint tells us that there is an error on line 8th of the Demo1.js file because a semicolon is missing.
If we make up the missing semicolon and run the command again, there will be no error message output.
Configure Jshint
Jshint has a default configuration to analyze your code, but its configuration settings are designed to be very flexible. There are four ways to provide configuration processing files for Jshint.
One way is --config
to use parameters to specify a configuration file:
Jshint demo1.js--config Config.json
Another way is to put the configuration in a file called. JSHINTRC, because Jshint will search for code analysis in the same level directory, if not found, it will continue to search the parent directory until the root directory, which allows us to set a different profile of a project.
The third way is to put the configuration information under the Jshintconfig attribute of the Package.json file.
In either of these three methods, the configuration information is JSON format that specifies each parameter to tell the Jshint option to turn on or off. For example, in the following configuration file, "unused" and "undef" are alarms that are used to activate unused and undefined variables. "Curly" requires that you always add braces to loops and conditional blocks. "Eqeqeq" means to prohibit the use of = = and!= and should use = = = and!==. "Globals" is used to specify a whitelist of global variables not defined in code.
The fourth way is to write configuration information to the code file in an annotated manner.
You can view different configuration options to control the behavior of Jshint.
A small example
Next, let's manipulate the options in the Config.json configuration file mentioned above. Let's say we have a JavaScript file like this, which is just a little piece of code for learning.
If we execute the jshint command demo2.js --config config.json
, we will get the following result:
There are 4 errors in our code. On line 9th Jshint should enclose the "if" code block with curly braces. The variable subscription_id
is defined but not used. In lines 9th and 11, "Confirm" and "console" are not defined.
We can avoid the top two errors with just a little modification:
Now, let's add a devel option to the Config.json file and set it to true so that Jshint can recognize "confirm" and "console."
Now, if we run the Jshint command again, there's nothing wrong with it.
Summarize
Jshint is a great tool for reducing code errors. Many editors provide jshint support. Just interested friends can further study jshint, the above is the use of jshint to reduce the entire content of JavaScript errors, I hope that the use of JavaScript can help.