require(...)不是函数

时间:2017-04-27 13:08:00

标签: sockets socket.io

我刚刚启动了Socket.IO,但我无法让它正常运行。她是代码

app.js

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
   res.sendFile('index.html');
});

http.listen(3000, function(){
   console.log("Listening On Port *: 3000");
});

的index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Hello World
    </body>
</html>

当我尝试使用node或nodemon运行它时,两者都已安装并且我已全局安装了socket.io(v1.7.3)它显示

TypeError: require(...) is not a function
    at Object.<anonymous> (I:\Bilal\Aptech\Tutorials Point Practice\Socket.IO\Chapter 1 - Hello World\app.js:1:91)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7)
    at startup (bootstrap_node.js:147:9)
    at bootstrap_node.js:538:3
[nodemon] app crashed - waiting for file changes before starting...

2 个答案:

答案 0 :(得分:0)

您可能没有快速安装,如果未安装必需('express')则返回null,这不是函数

修复运​​行

npm install express

答案 1 :(得分:0)

我认为这是因为您尝试在最后使用()作为函数调用require。试试这个:

var app = require('express');

否则,试试这个:

var express = require('express'); var app = express();