4

console.log does not work for me when I place it in a function in public/javascripts folder.

My current node.js folder structure is:

  • views
  • routes
  • public
    • javascripts > utils.js

In utils.js, i have this function

function goToGoogle () {
  alert ('Hello World');
  console.log('Hello World');
}

The alert() command works fine, but not the console.log

I've tried the console.log in other js files in the routes/ directory and it outputs to the terminal just fine.

Any idea? Thanks

Michael
  • 3,699
  • 4
  • 26
  • 27

2 Answers2

6

If the code is in public then I assume it runs in the browser. That means messages will come up in the browser's console, not Node's console. In Chrome for instance you go to Tools -> Javascript Console in the menu.

The reason it works in other files is because the JS for routes runs in Node and thus uses Node's console.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • yes, i just Chrome View->Developer->Javascript Console and it works great! Thanks!!! – Michael May 01 '13 at 03:56
  • Why `console.log` is only coming in Node's console? I am using Shell Client(SSH) as Node's console. It doesn't outputs `console.info`, `console.warn` etc. Why this happens? I think, `alert` will not support in Shell client. – Justin John May 01 '13 at 10:07
  • @JustinJohn I'm sorry I don't understand what you are asking. The source of the issue here is that the browser and Node are two separate JavaScript execution environments. If the code runs in the browser via a ` – loganfsmyth May 01 '13 at 15:47
  • What I asked is whether Node's terminal support all `console` statements in it? From my experience, I get is `console.log` is supported, but rest `console.warn`, `console.info` etc are not supported in node's terminal. – Justin John May 01 '13 at 16:04
  • @JustinJohn They working for me on 0.10.0, do you have an old version of Node? If you can, please make a new question since this isn't related to my answer here. – loganfsmyth May 01 '13 at 16:19
0

I have written a solution for users of nodejs in ssh-environment. Because there is no included browser, you can use firebase (https://firebase.google.com) for realtime-console.log() in your browser.

Output to Chrome console from Node.js

Community
  • 1
  • 1
Marcel Ennix
  • 1,328
  • 1
  • 12
  • 16