node.js 在 JSHint 中禁用有关“require”功能的警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15894193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:09:33  来源:igfitidea点击:

Disabling warning about "require" function in JSHint

node.jsrequirejshint

提问by somesh

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

我正在为 Node.js 编写一些代码,目前我正在使用 JSHint 检查我的代码。但是,当我使用该require函数导入模块时,它说:

'require' is not defined.

“要求”未定义。

How can I suppress the warning?

我怎样才能抑制警告?

"use strict";
var express = require('express');   
var register = require('./routes/register');

回答by generalhenry

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

jshint 默认不知道 node.js 全局变量,您需要通知它。

add this comment to the top:

将此评论添加到顶部:

/* jshint node: true */

/* jshint node: true */

回答by Sridhar

We can set nodeas the global environment variable in JSHint's .jshintrcfile

我们可以node在 JSHint 的.jshintrc文件中设置为全局环境变量

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.

当您的代码在 Node 运行时环境中运行时,此选项定义可用的全局变量。Node.js 是使用异步事件驱动模型的服务器端 JavaScript 环境。此选项还会跳过一些在浏览器环境中有意义但在 Node 中没有意义的警告,例如文件级 use strict pragmas 和 console.log 语句。

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

有关更多信息http://jshint.com/docs/options/#node

{
   "node": true
}

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

'require' is not defined, 'console' is not defined, 之类的错误'module' is not defined不会再出现

回答by Vlad Bezden

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

您可以通过在.jshintrc文件中添加“require”来配置 JSHint 。例如:

{
    "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

有关如何配置 JSHint 的更多信息,请阅读JSHint 文档

回答by jaredbaszler

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

我偶然发现了这个答案,但在 VSCode 中什么也做不了。我得到了这个工作:

  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.jsonfile:

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

  1. 使用Preferences: Open Settings (JSON)通过命令面板窗口 (Ctrl-Shift-P)打开 JSON 设置文件。
  2. 将此部分添加到settings.json文件中:

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