【问题标题】:Node js, module.js: 340 Error: cannot find module 'tcp'节点 js,module.js:340 错误:找不到模块 'tcp'
【发布时间】:2014-11-07 15:12:45
【问题描述】:

我是 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();
    }); 

【问题讨论】:

  • 您想发布更多代码吗?
  • @huan feng,我试过了,然后我得到了第二个错误
  • @ryanc1256, var sys = require("sys"), tcp= require("tcp");

标签: javascript node.js


【解决方案1】:

好的,首先
模块 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);
        });
    }
}

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

【讨论】:

  • 谢谢 ryanc1256,我会试试...这个问题正在消耗我的大脑。
猜你喜欢
  • 2013-09-29
  • 1970-01-01
  • 2015-03-26
  • 2013-01-26
  • 2018-12-03
  • 2019-12-24
  • 2018-08-06
  • 2014-05-13
  • 1970-01-01
相关资源
最近更新 更多