In my nwjs app, I load a _launch.js file from an HTML file:
<html>
<body>
<script type="text/javascript" src="_launch.js"></script>
</body>
</html>
And in my _launch.js file, I fire up the the Node processes I need for an express server and socketIO.
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
gui = require('nw.gui'),
__curDir = process.cwd(),
//keep the logic for the IO connections separate
ioServer = require(__curDir + '/server.js');
//configure Express to default web requests to /workspace/ folder
app.use(express.static(__curDir + '/workspace'));
ioServer.init(io, console);
server.listen(3000, function () {
console.log('HTTP server listening on *:3000');
window.location = 'http://localhost:3000/MyApp/';
});
The app launches just fine, and my express/socketIO connections are all perfectly working.
But while the console.log() in the server.listen() callback appears in my terminal, any messages I try to log from the server.js file (required earlier) never show up anywhere.
Any ideas why?
Per the nwjs wiki, any files loaded via require() should be running in the Node context (and mine otherwise appears to be) -- but for whatever reason, I cannot use console.log() to view logged information.
Enable view of the Terminal and Debug Console (go to the View menu and select Terminal). You can press F5 for Debugging or Ctrl-F5 for Run without debugging. When you get a menu asking for environment, select Node. js.
console. log('Inside Global Execution Context'); An Execution Context Stack for the above code. When the above code loads in the browser, the Javascript engine creates a global execution context and pushes it to the current execution stack.
console. log() is a function used to print information to the console. return on the other hand is a call to pass some value back up to where the call was made. For instance, let's say you create a function called square() that takes in a single numeric parameter and returns that value squared.
You have to run DevTools for Background Page by right clicking on your app and choosing the Inspect background page option from the context menu.
click for full size image
Here is a related bug report: 0.13-beta3 console.log doesn't work in node context
Just pass --enable-logging=stderr
in chromium-args
in your package.json:
{
...
"chromium-args": "--enable-logging=stderr",
...
}
Or use the Inspect background page
DevTools as @Jakub Bejnarowicz has pointed out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With