DJG DJG - 1 month ago 6
Javascript Question

Why isn't my node app logging to the terminal or the Chrome console?

I'm a noob when it comes to back-end with Node.

I'm running this file on an Ubuntu 14 server in the cloud. The file is being run with pm2. (ex. pm2 start newServer). The outputs from the console.log()s aren't appearing either in the terminal I've ssh'd into or in the javascript console in the browser. How can I get the file to log output into the terminal I've ssh'd into? Or am I going about this the wrong way entirely (do I need to start learning unit testing, is there some logging module I should be using, ...?)

EDIT: The pm2 logs (viewed with 'pm2 logs' or 'pm2 logs [app name]) don't contain any of the output from the console.log statements in the node script.

EDIT2: I also don't get log output anywhere when I stop the pm2 process and just run the file with node.

result of running with node

EDIT3: Still no output when I run the script with node on my local machine and access the page in the browser via localhost:8080.

FINAL EDIT: Figured it out. It's because my gulpfile had strip-debug in it. Being new to build systems, I didn't realize that said option removed console.logs. I found out by using node-inspector to actually look at the node code that was being run, and finding that my console.log statements had been replaced with "void 0"s.

var express = require('express'); //Require express for middleware use
const app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http); //IO is the server

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');

//BASICS
app.use(express.static(__dirname + "/served")); //Serve static files

app.get('/', function(req, res){
res.sendFile('./index.html');

});

io.on('connection', function(socket){
// socket.on('chat message', function(msg){
// io.emit('chat message', msg);
// });
console.log("A user connected: ", socket.id);
});

http.listen(8080, function(){
console.log('listening on *:8080');
});

console.log('test')

// MONGODB
var url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server.");
db.close();
});

Answer

I'm not an expert on pm2, but from a quick glance it's designed for production use, and won't log anything to the terminal.

Looking at the page

https://github.com/Unitech/pm2

There is a section on Log facilities which tells you how to look at the logs, with a command like this

pm2 logs APP-NAME       # Display APP-NAME logs

Otherwise if you really just want to see what it is doing, just run your code with node, eg

node myprogram.js
Comments