javascript 在定义之前使用了“require”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27844208/
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-10-28 08:04:36  来源:igfitidea点击:

'require' was used before it was defined

javascriptnode.js

提问by Kevin

I'm starting to study Node.js. I purchased the manual written by Marc Wandscheider. I downloaded the tools to use it and I also downloaded Brackets.

我开始学习 Node.js。我购买了 Marc Wandscheider 编写的手册。我下载了使用它的工具,还下载了 Brackets。

I'm trying a sample script, but I get two errors that do not understand and that are not present in the guide.

我正在尝试一个示例脚本,但我收到两个不明白且指南中没有的错误。

The first error tells me that:

第一个错误告诉我:

'require' was used before it was defined

在定义之前使用了“require”

C:\node> node debug web.js
<Debugger listening on port 5858>
connecting ... ok
break in C:\node\web.js: 1
?? 1 var http = require ("http");
?? 2
?? 3 process_request function (req, res) {
debug>

while the second (in Brackets):

而第二个(在括号中):

missing use strict statement

缺少 use strict 语句

I saw on the internet that I can add the line

我在网上看到可以加行

"use strict";

But the guide does not use it - is it required?

但该指南不使用它 - 是否需要?

How can I fix these issues?

我该如何解决这些问题?

entire code

完整代码

var http = require("http");

function process_request(req, res) {

    var body = 'Thanks for calling!';
    var content_length = body.length;
        res.writeHead(200, {
            'Content-Length': content_length,
            'Content-Type': 'text/plain'
        });
    res.end(body);
}

var s = http.createServer(process_request);
s.listen(8080);

回答by pdjota

Those errors are actually suggestions of the JSHINT process to validate good code. Brackets is using it behind the scene probably. If you tell jshint you are writing for node then requirebecomes a global variable so it does not give that error. Try running this code with some warnings for JSHINTsome article with proper explanation on using JSHINT

这些错误实际上是 JSHINT 过程验证良好代码的建议。Brackets 可能在幕后使用它。如果您告诉 jshint 您正在为 node 编写,那么require将成为一个全局变量,因此它不会给出该错误。尝试运行了一些警告,该代码JSHINT一些文章用适当的解释使用JSHINT

/*jshint node:true */
'use strict';
var http = require('http');

function process_request(req, res) {

    var body = 'Thanks for calling!';
    var content_length = body.length;
        res.writeHead(200, {
            'Content-Length': content_length,
            'Content-Type': 'text/plain'
        });
    res.end(body);
}

var s = http.createServer(process_request);
s.listen(8080);

回答by Malice

I encountered similar warnings from the JSLint window in Brackets while writing a gulp file. This is how I resolved them:

在编写 gulp 文件时,我在 Brackets 中的 JSLint 窗口中遇到了类似的警告。我是这样解决它们的:

Problems × 1 'require' was used before it was defined.

Problems × 1 'require' was used before it was defined.

The require function is defined elsewhere, specifically as part of Node.js so to resolve the warning, mark it as a global by adding this at the top of your JavaScript file:

require 函数在别处定义,特别是作为 Node.js 的一部分,以便解决警告,通过在 JavaScript 文件的顶部添加以下内容将其标记为全局:

/* global require */

See http://jslinterrors.com/a-was-used-before-it-was-defined

http://jslinterrors.com/a-was-used-before-it-was-defined

Missing 'use strict' statement

Missing 'use strict' statement

I resolved this by using an immediately invoked function expression:

我通过使用立即调用的函数表达式解决了这个问题:

(function () {
    "use strict";

    // The rest of the code
}());

See http://jslinterrors.com/move-the-invocation-into-the-parens-that-contain-the-function

http://jslinterrors.com/move-the-invocation-into-the-parens-that-c​​ontain-the-function

Combine this with the previous 'var' statement

Combine this with the previous 'var' statement

This one is straightforward. Instead of

这是直截了当的。代替

var body = 'Thanks for calling!';
var content_length = body.length;

use

利用

var body = 'Thanks for calling!',
    content_length = body.length;