0

I'm trying to connect my Android project with Leap motion controller and am using the procedure and source mentioned on this question as reference.

I'm very new to Node.js and am unable to get the JavaScript file to work with Node and am unable to resolve dependencies. I tried node update but that didn't work. I need to resolve the following error:

$ node index.js 
module.js:338
    throw err;
          ^
Error: Cannot find module 'sleep'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/index.js:11:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

Here's the JavaScript file for reference:

var http = require('http'), express = require('express'), app = express(), server = http
        .createServer(app), port = process.env.PORT || 5000, position = {
    x : 0,
    y : 0,
    z : 0,
    hand : '',
    present : false
};
var updated = false;

var sleep = require('sleep');

server.listen(port, function() {
    console.log('Listening on ' + port);
});

function hasUpdate() {
    if (updated) {
        updated = false;
        return true;
    }
    return false;
}

var BinaryServer = require('binaryjs').BinaryServer;
var binaryServer = new BinaryServer({
    port : 9000

});
binaryServer.on('connection', function(client) {
    console.log('accept binary connection');
    var stream = client.createStream();
    var buffer = [];
    console.log('got stream');
    stream.on('data', function(data) {
        buffer.push(data);
        var text = buffer.join();
        var end = text.lastIndexOf('}');
        if (end > -1) {
            var temp = text.substr(0, end+1);
            var start = temp.lastIndexOf('{');
            if (start > -1) {
                var message = text.substr(start, end+1);

                var o=JSON.parse(message);
                updateHandPosition(o);

                if (end +1 < buffer.length) {
                    buffer = buffer.splice(end + 1, buffer.length);
                }else{
                    buffer=[];
                }
            }
//          console.log('received data from client ' + message + ' ' + end
//                  + ' ' + start + ' ');

        }
    });
});

app.use(express.bodyParser());

app.get('/', function(request, response) {
    response.sendfile('public/index.html');
});

function updateHandPosition(o){
    position.present = o.present;
    if (position.present) {
        position.x = o.x;
        position.y = o.y;
        position.z = o.z;
        position.hand = o.hand;
    } else {
        position.x = null;
        position.y = null;
        position.z = null;
        position.hand = null;
    }
    updated=true;
//  console.log('updating hand position '+JSON.stringify(position));
}

app.post('/update', function(request, response) {
    updateHandPosition(response.body);
    response.json({
        success : true
    });
    updated = true;
});

app.get('/retrieve', function(request, response) {
    response.json(getPositionData());
});

function getPositionData() {
    if (position.present) {
        return {
            present : true,
            x : position.x,
            y : position.y,
            z : position.z,
            hand : position.hand
        };
    } else {
        return {
            present : false
        };
    }
}

function forever(request, response) {
    this.response = response;
    this.request = request;

    var ref = this;
    this.request.on('close', function() {
        ref.clear();
    });
    this.request.on('end', function() {
        ref.clear();
    });
};

forever.prototype.clear = function() {
    this.request = null;
    this.response = null;
};
forever.prototype.stream = function() {
    if (this.response != null) {
        if (hasUpdate()) {
            this.response.write(JSON.stringify(getPositionData()));
            this.response.write(",");
        }
        var ref = this;
        setTimeout(function() {
            ref.stream();
        }, 5);
    }

};

app.get('/stream', function(request, response) {
    var f = new forever(request, response);
    f.stream();
});

app.get(/^(.+)$/, function(req, res) {
    res.sendfile('public/' + req.params[0]);
});

I tried using "node install sleep" but I get this error log:

npm WARN package.json LeapCallController@0.1.0 No repository field.
/
> sleep@2.0.0 install /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/node_modules/sleep
> node-gyp rebuild

gyp ERR! configure error 
gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag instead
gyp ERR! stack     at install (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:66:16)
gyp ERR! stack     at Object.self.commands.(anonymous function) [as install] (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/node-gyp.js:66:37)
gyp ERR! stack     at getNodeDir (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:152:20)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:95:9
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:723:7)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at maybeClose (child_process.js:1000:16)
gyp ERR! stack     at Socket.<anonymous> (child_process.js:1168:11)
gyp ERR! stack     at Socket.emit (events.js:107:17)
gyp ERR! stack     at Pipe.close (net.js:461:12)
gyp ERR! System Linux 3.16.0-30-generic
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/node_modules/sleep
gyp ERR! node -v v0.13.0-pre
gyp ERR! node-gyp -v v1.0.1
gyp ERR! not ok 

npm ERR! sleep@2.0.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sleep@2.0.0 install script.
npm ERR! This is most likely a problem with the sleep package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls sleep
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 3.16.0-30-generic
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "sleep"
npm ERR! cwd /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent
npm ERR! node -v v0.13.0-pre
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm ERR! not ok code 0
halfer
  • 19,824
  • 17
  • 99
  • 186
Darth Coder
  • 1,738
  • 7
  • 33
  • 56

1 Answers1

2

If you are not using sleep remove this line

var sleep = require('sleep');

or install the sleep module using

npm install sleep

and restart the node project

zoram
  • 512
  • 6
  • 16
  • 1
    Well installing the sleep module didn't work for me (as indicated by the error log in the question) but I did comment the line and then had to install another module (binaryjs) which installed successfully. I can run the script now but any information on how I could have dealt with "sleep" installation would be helpful. – Darth Coder Mar 05 '15 at 22:05