111

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/');
chrismanderson
  • 4,603
  • 5
  • 31
  • 47
  • 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 Dec 03 '11 at 01:37
  • I can't replicate this on my machine. This works just fine. What version are you running? – Sean Hill Dec 03 '11 at 01:39
  • 3
    console.log() emits the strings into the terminal window (the command line interface where the application is ran) – Pastor Bones Dec 03 '11 at 01:42
  • how did you start the server, without noticing the output in the console ? – RobertPitt Dec 03 '11 at 01:52
  • 3
    You have no idea how dumb I feel over this :). – chrismanderson Dec 03 '11 at 02:55

3 Answers3

177

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.

david
  • 17,925
  • 4
  • 43
  • 57
  • 105
    I would down vote my own question if I could. Cannot believe I did not make this connection. – chrismanderson Dec 03 '11 at 02:55
  • 6
    everyone staring with nodejs expects the same... the console output should log to the browser's console, as well as the terminal. – Brad Parks Jan 02 '14 at 03:52
  • 19
    @Brad No it shouldn't. Not all node programs are web servers, and even if they were it still wouldn't make sense. – david Jan 05 '14 at 04:42
  • 6
    @David - I get your point... I know that usually server side stuff would never log directly to the client log... but node.js is javascript, which makes people think client side, to some extent... and since the logging method has exactly the same name as the client side one, "console.log", then i think it follows that *some* people would think it might log to the browsers console. – Brad Parks Jan 06 '14 at 01:22
  • @BradParks, @david Yes, I was also looking for the output in the browser console on the usage of `console.log()`, then I reached to this Q/A. – Sithu Nov 25 '14 at 08:08
  • Stuff can be logged on both, I was just now confused by it. Because node.js can also serve client side code which then is, of course, logged in the browser. Need to check both, for the full story. I wish browser log could be relayed to the terminal. – Ralf Jul 16 '18 at 18:37
  • wat, Node.js doesn't run in the browser. Why would it make sense to output logs to the browser? – kybak Mar 18 '21 at 18:45
10

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

atomless
  • 1,272
  • 15
  • 18
  • The github project does not exist anymore – yan May 21 '17 at 18:02
  • 7
    Also, this is probably going down the wrong path. Logging node server activito the the browser console is just a weird practice. I can think of zero times where this would be genuinely useful. To be totally honest, trying to log from server to client in the console sounds like a lack of understanding of how software should be built. I don't mean that in a mean way, it just is a basic separation of concerns concept. Just because they both use javascript doesn't mean they are the same ENVIRONMENT. There's a very important distinction there. – dudewad Mar 27 '18 at 18:41
  • @dudewad I kind of agree but it seems like a nice convenience that can be used while developing. – Ruan Mendes Feb 16 '22 at 17:08
8

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

J Decker
  • 537
  • 5
  • 9