Nw.js: node-main: errors and console.log not redirected to webkit console

Created on 27 Feb 2014  ·  15Comments  ·  Source: nwjs/nw.js

tested with nw version: 0.8 and 0.9.2 on windows

We had errors in our script that is used as node-main. Unfortunately neither the errors nor console.log appeared in the webkit console. Not even in the terminal.

We could only retrieve the errors by logging them to file, having a big try/catch block around everything.

Apart from this bug, I would propose two new options for package.json, one for logging everything (errors, console.whatever from windows and node-main) to a log-file and one for logging everything to the terminal.

Thanks a lot. Great tool! :)

p.s. Linux shows errors in terminal (on windows nw seems to create a child process)

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

needinfo

All 15 comments

try window.console.log.. See #1218 for the rest :)

window.console.log doesn't work at the beginning of the node-main-script, see node-main:

window: [...] This symbol is not available at the time when the script is loaded, because the script is executed before the DOM window load.

Also this wouldn't really help with errors or node modules using console.log, though a workaround could be to redirect console.log to window.console.log...

Thank you anyway!

Well that's true, not much you can do about that. Though if you launch the app via CLI it _should_ appear in the output.

try using global.console.log
I had similar problems during development of my ap with not getting console output, allthough I'm not using a node-main script. Since then I'm only using global.console.log for log output.
I also did a quick test (just a timeout function writing every second to console) with a node-main script and it worked. So try it...

I'm sorry but I've tried global.console.log but it doesn't work when using node-main field.

I have temporary 2 solutions to output logs on terminal.

  1. Util - http://nodejs.org/docs/latest/api/util.html#util_util_format_format
var util = require('util');
util.log('log message');
util.log(util.inspect({'test': 'test'}));
  1. Log module - https://www.npmjs.org/package/log
var Log = require('log')
  , log = new Log('debug');

log.info('sending email', {'test': 'test'});
log.error('failed to send email');
log.debug('preparing');

Hope that helps.

What do you think about this workaround? I'm waiting for the standard console to become usable.

function waitForConsoleThenStart() {
  if (typeof global.window !== 'undefined') {

    // Here you can use console.log
    console.log("test");

  } else {
    setTimeout(waitForConsoleThenStart, 100);
  }
}
waitForConsoleThenStart();

I cannot get any of the proposed work arounds to work in 0.13.1. Is there anyway to output to STDOUT or STDERR messages for debugging node-main scripts?

you may try
process.stdout.write(util.format.apply(util, arguments));

which would look like

var console = (function(){
    var util = require('util');
    return {
        log:function(x){
            process.stdout.write(util.format.apply(util, arguments));
        }
    }
})()
//...
console.log('test', {whatever:'test'})

Thanks @mpech , I will give this a try!!!

None of the solutions have worked for me, however I'm running the application through an IDE (using an NPM script) so there is more potential for issues there. I've tried all the suggestions in this thread to no avail (including using process.stdout/stderr).

Actually, console.log is ok. You should check the log in the app with mouse right click, and choose the last item(检查背景页(B) which in English may be check background(B)), and it shows the log.

@coolguy001tv
really wish the problem is that simple, but the fact is , we know how to open the devTools.

I try a simple test on nwjs-sdk-v0.26.6, the error in node.js script(node-main) could be shown in the DevTools for background page.
Does it still reproduce for you? If so, could you please provide a sample and steps to reproduce it?

close until there is more information

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rumax picture rumax  ·  4Comments

loretoparisi picture loretoparisi  ·  3Comments

chino23 picture chino23  ·  3Comments

sorty-zz picture sorty-zz  ·  4Comments

niutech picture niutech  ·  4Comments