我正在尝试将我的Android项目与Leap运动控制器连接,并使用this question上提到的程序和来源作为参考。
我是Node.js的新手,无法让JavaScript文件与Node一起使用,无法解析依赖关系。我试过了node update
但是没有用。有人可以帮我解决以下错误:
$ 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)
以下是供参考的JavaScript文件:
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]);
});
非常感谢任何帮助。
我尝试使用“node install sleep”但我收到此错误日志:
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
答案 0 :(得分:1)
如果你没有使用睡眠,请删除此行
var sleep = require(&#39; sleep&#39;);
或使用
安装睡眠模块npm install sleep
并重新启动节点项目