chrismanderson
chrismanderson

Reputation: 4813

Node.js console.log() not logging anything

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/');

Upvotes: 117

Views: 220245

Answers (4)

david
david

Reputation: 18258

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.

Upvotes: 185

Ramji Pal
Ramji Pal

Reputation: 3

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

Upvotes: 0

J Decker
J Decker

Reputation: 586

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

Upvotes: 8

atomless
atomless

Reputation: 1282

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

Upvotes: 10

Related Questions