简体   繁体   English

在定义之前使用'require'

[英]'require' was used before it was defined

I'm starting to study Node.js. 我开始研究Node.js. I purchased the manual written by Marc Wandscheider. 我购买了Marc Wandscheider编写的手册。 I downloaded the tools to use it and I also downloaded Brackets. 我下载了使用它的工具,我也下载了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 缺少使用严格声明

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);

Those errors are actually suggestions of the JSHINT process to validate good code. 这些错误实际上是JSHINT流程验证优秀代码的建议。 Brackets is using it behind the scene probably. Brackets可能正在幕后使用它。 If you tell jshint you are writing for node then require becomes a global variable so it does not give that error. 如果您告诉jshint您正在为节点编写,那么require将成为一个全局变量,因此它不会给出该错误。 Try running this code with some warnings for JSHINT some article with proper explanation on using JSHINT 尝试运行了一些警告,该代码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);

I encountered similar warnings from the JSLint window in Brackets while writing a gulp file. 我在编写gulp文件时遇到了来自Brackets的JSLint窗口的类似警告。 This is how I resolved them: 这就是我解决它们的方法:

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

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

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM