节点js,module.js:340错误:找不到模块' tcp'

时间:2014-11-07 06:02:29

标签: javascript node.js

我是node js编程的新手,我正在开发一个想要为机器人公司创建服务器的程序。我有运行他们发送给我的服务器的文件。现在的问题是当我运行服务器时显示错误

module.js:340
    throw err;
          ^
Error: Cannot find module 'tcp'

    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (D:\Users\Vishnu Mohan\Projects\website\libs\node.websocket.js\lib\redis.js:7:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

所以我把tcp更改为net(来自堆栈溢出的提示)然后我再次运行。现在我得到另一个错误

    this.conn = new process.tcp.Connection();
                               ^
TypeError: Cannot read property 'Connection' of undefined

    at Client.connect (D:\Users\Vishnu Mohan\Projects\website\libs\node.websocket.js\lib\redis.js:47:32)
    at Object.<anonymous> (D:\Users\Vishnu Mohan\Projects\website\libs\node.websocket.js\log.js:66:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (D:\Users\Vishnu Mohan\Projects\website\libs\node.websocket.js\websocket.js:14:14)
    at Module._compile (module.js:456:26)

即时通讯使用最新版本的节点v0.10.33

var sys = require("sys"), 
    tcp= require("tcp");

Client.prototype.connect = function (callback_on_connect, callback_on_error) {
  var self = this;
  if (this.conn && this.conn.readyState === "open") {
    if (typeof(callback_on_connect) === "function")
      callback_on_connect();
  } else {
    this.conn = new process.tcp.Connection();
    this.conn.addListener("connect", function () {
      this.setEncoding("binary");
      this.setTimeout(0);          // try to stay connected.
      this.setNoDelay();
      if (typeof(callback_on_connect) === "function")
        callback_on_connect();
    }); 

1 个答案:

答案 0 :(得分:0)

好的,首先是 模块tcp现在称为“net”,这里是他们的文档 http://nodejs.org/api/net.html#net_new_net_socket_options

只是通过阅读文档我改变了你的代码......

var sys = require("sys"), 
    tcp= require("net");
var port = 80;

Client.prototype.connect = function (callback, error){
    var self = this;
    if (this.conn && this.conn.readyState === "open") {
        if (typeof(callback) === "function")
            callback();
    } else {
        this.conn = new net.Socket();

        this.conn.connect(port, function() {
            this.setEncoding('binary');
            this.setTimeout(0);
            this.setNoDelay();
        });


        this.conn.on('connect', function(){
          console.log('something connected!');
        });

        this.conn.on('error', function(error){
               error();
                console.log(err);
        });
    }
}

我还没有尝试过,但我想这一点是你正确的方向