116

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

4 Answers 4

183

In a node.js server console.log outputs to the terminal window, not to the browser's console window. You should see the output directly after you start it.

7
  • 107
    I would down vote my own question if I could. Cannot believe I did not make this connection. Commented Dec 3, 2011 at 2: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
    Commented Jan 2, 2014 at 3: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
    Commented Jan 5, 2014 at 4: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
    Commented Jan 6, 2014 at 1: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
    Commented Nov 25, 2014 at 8:08
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

3
  • The github project does not exist anymore
    – yan
    Commented May 21, 2017 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
    Commented Mar 27, 2018 at 18:41
  • @dudewad I kind of agree but it seems like a nice convenience that can be used while developing. Commented Feb 16, 2022 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

0

I was also encountring the same problem. my code was simple just printing "hello word" using console.log() after running node hello.js . i don't get any output or error it just can give me any output then i read this article Why does node.js need python

which says you have to install python to run javascript programms in local machine so i install the python in my locale and run some programms of python so that it is build in my machine then after doing all this stuff when i run node -hello.js it works and give me desired output

Not the answer you're looking for? Browse other questions tagged or ask your own question.