96
votes

Trying out node.js for the first time. Set up node, set up the example app from the nodejs.org site. Can start the server fine, but console.log() isn't actually logging anything. Tried the Javascript console in Chrome, Firefox, and Safari - nothing appears in the log. Also checked Console on my Mac just for kicks, nothing was there either. What am I missing?

(Here's the example code that works but doesn't log anything.)

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
3
How are you running this app? What environment? If it is on the console on your local machine then you should see the logs coming from the app there. If you are using a hosted solution then they usually provide some way to view the logs through a web interface and through their command line API tool.Treffynnon
I can't replicate this on my machine. This works just fine. What version are you running?Sean Hill
console.log() emits the strings into the terminal window (the command line interface where the application is ran)Pastor Bones
how did you start the server, without noticing the output in the console ?RobertPitt
You have no idea how dumb I feel over this :).chrismanderson

3 Answers

163
votes

In a node.js server console.log outputs to the terminal window, not to the browser's console window.

How are you running your server? You should see the output directly after you start it.

9
votes

This can be confusing for anyone using nodejs for the first time. It is actually possible to pipe your node console output to the browser console. Take a look at connect-browser-logger on github

UPDATE: As pointed out by Yan, connect-browser-logger appears to be defunct. I would recommend NodeMonkey as detailed here : Output to Chrome console from Node.js

8
votes

Using modern --inspect with node the console.log is captured and relayed to the browser.

node --inspect myApp.js

or to capture early logging --inspect-brk can be used to stop the program on the first line of the first module...

node --inspect-brk myApp.js