IntelliJ中JSLint不识别.jshintrc内配置的全局变量问题求助
Hey there, let's break down why those JSLint errors keep popping up even with your .jshintrc set up properly. The key thing to remember here is that JSLint and JSHint are separate tools with completely different configuration systems—your .jshintrc settings don't apply to JSLint at all, which is why you're still seeing those warnings.
Here's how to resolve the issue:
1. Core Reason for the Problem
JSHint relies on .jshintrc for project-wide configuration, but JSLint doesn't recognize this file whatsoever. It has its own distinct way of handling environment flags and global variables, so your current setup doesn't register with JSLint.
2. Quick Fix: File-Level JSLint Comments
Since you noticed adding /*globals console */ works, you can expand this to cover all your needs in one go:
- For built-in environment globals (like
consolefrom the browser, or$from jQuery), use JSLint's environment-specific comments:/*jslint browser: true, jquery: true */ - For your custom globals (
swal,testvar1, etc.), add a global comment alongside it:/*jslint browser: true, jquery: true */ /*global swal, testvar1, testfunction1, testfunction2, testvar2 */
Drop these lines at the very top of each file JSLint checks, and it should stop flagging those undeclared variables/functions.
3. Project-Wide JSLint Configuration
If you don't want to add comments to every file, set up a project-wide JSLint config:
- Create a file named
jslint.conf(or.jslintrc, depending on your editor/plugin) in your project root. - Add your configuration in valid JSON format—here's how it should look for your setup:
{ "browser": true, "jquery": true, "globals": { "swal": true, "testvar1": true, "testfunction1": true, "testfunction2": true, "testvar2": true } } - Then, configure your editor's JSLint plugin to read this file. For example, in VS Code, you'd adjust the JSLint extension settings to point to your config file's path.
4. Sanity Check for JSHint
While your main issue is with JSLint, a quick check to ensure JSHint is working as expected:
- Confirm your
.jshintrcis in the project root (not a subfolder) and has valid JSON syntax. Here's a correct example for your setup:{ "browser": true, "jquery": true, "node": true, "globals": { "swal": true, "testvar1": true, "testfunction1": true, "testfunction2": true, "testvar2": true } } - Make sure your editor's JSHint plugin is enabled and set to use the project's
.jshintrc.
内容的提问来源于stack exchange,提问作者Jeya Kumar

