Nodejs + certbot + expressJS - Error: Cannot find module 'socket.io'?
Nodejs + certbot + expressJS - Error: Cannot find module 'socket.io'?
我已经在全局安装了 socket.io,正如这个答案所说:
npm install -g socket.io
但为什么我仍然收到错误:
www-0 (err): at Function.Module._load (module.js:388:25)
www-0 (err): at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err): at Module.require (module.js:468:17)
www-0 (err): at require (internal/module.js:20:19)
www-0 (err): at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err): at Module._compile (module.js:541:32)
www-0 (err): at Object.Module._extensions..js (module.js:550:10)
www-0 (err): at Module.load (module.js:458:32)
www-0 (err): at tryModuleLoad (module.js:417:12)
www-0 (err): Error: Cannot find module 'socket.io'
www-0 (err): at Function.Module._resolveFilename (module.js:440:15)
www-0 (err): at Function.Module._load (module.js:388:25)
www-0 (err): at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err): at Module.require (module.js:468:17)
www-0 (err): at require (internal/module.js:20:19)
www-0 (err): at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err): at Module._compile (module.js:541:32)
www-0 (err): at Object.Module._extensions..js (module.js:550:10)
www-0 (err): at Module.load (module.js:458:32)
www-0 (err): at tryModuleLoad (module.js:417:12)
这是我的 bin/www:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('mongoose-iot:server');
var http = require('http');
// Add HTTPS support.
// https://www.hacksparrow.com/express-js-https.html
//
// http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/
var https = require('https');
var fs = require('fs');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Get port from environment and store in Express.
*/
var httpsPort = normalizePort(process.env.PORT || '3030');
app.set('port', httpsPort);
/**
* Create HTTPS server.
*/
// Generate self-signed certificate.
// $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem
// http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/
//
// https://startupnextdoor.com/how-to-obtain-and-renew-ssl-certs-with-lets-encrypt-on-node-js/
var sslPath = '/etc/letsencrypt/live/example.co.uk/';
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
};
var httpsServer = https.createServer(options, app);
/**
* Listen on provided port, on all network interfaces.
*/
httpsServer.listen(httpsPort);
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
我必须 require
socket.io 吗?
尝试使用 require('socket.io')
将 socket.io 模块添加到您的文件中
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(client){
client.on('event', function(data){});
client.on('disconnect', function(){});
});
server.listen(3000);
我已经在全局安装了 socket.io,正如这个答案所说:
npm install -g socket.io
但为什么我仍然收到错误:
www-0 (err): at Function.Module._load (module.js:388:25)
www-0 (err): at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err): at Module.require (module.js:468:17)
www-0 (err): at require (internal/module.js:20:19)
www-0 (err): at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err): at Module._compile (module.js:541:32)
www-0 (err): at Object.Module._extensions..js (module.js:550:10)
www-0 (err): at Module.load (module.js:458:32)
www-0 (err): at tryModuleLoad (module.js:417:12)
www-0 (err): Error: Cannot find module 'socket.io'
www-0 (err): at Function.Module._resolveFilename (module.js:440:15)
www-0 (err): at Function.Module._load (module.js:388:25)
www-0 (err): at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err): at Module.require (module.js:468:17)
www-0 (err): at require (internal/module.js:20:19)
www-0 (err): at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err): at Module._compile (module.js:541:32)
www-0 (err): at Object.Module._extensions..js (module.js:550:10)
www-0 (err): at Module.load (module.js:458:32)
www-0 (err): at tryModuleLoad (module.js:417:12)
这是我的 bin/www:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('mongoose-iot:server');
var http = require('http');
// Add HTTPS support.
// https://www.hacksparrow.com/express-js-https.html
//
// http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/
var https = require('https');
var fs = require('fs');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Get port from environment and store in Express.
*/
var httpsPort = normalizePort(process.env.PORT || '3030');
app.set('port', httpsPort);
/**
* Create HTTPS server.
*/
// Generate self-signed certificate.
// $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem
// http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/
//
// https://startupnextdoor.com/how-to-obtain-and-renew-ssl-certs-with-lets-encrypt-on-node-js/
var sslPath = '/etc/letsencrypt/live/example.co.uk/';
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
};
var httpsServer = https.createServer(options, app);
/**
* Listen on provided port, on all network interfaces.
*/
httpsServer.listen(httpsPort);
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
我必须 require
socket.io 吗?
尝试使用 require('socket.io')
将 socket.io 模块添加到您的文件中var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(client){
client.on('event', function(data){});
client.on('disconnect', function(){});
});
server.listen(3000);