Skip to content

Changes related to node

Cong Liu edited this page Mar 30, 2016 · 16 revisions

NOTE: some content in this wiki applies only to 0.12 and earlier versions. For official documentation on 0.13 and later, see http://docs.nwjs.io

This document is still under construction

console

Since node-webkit supports GUI applications instead of console applications, the output of console.log() (and other similar methods such as console.warn() and console.error()) is redirected to WebKit's console. You may see it in your “Developer Tools” window (on its “Console” tab).

process

A couple of new fields is added to the global process object:

  • process.versions['node-webkit'] is set with node-webkit's version.
  • process.versions['chromium'] is set with the chromium version which node-webkit is based on. (since v0.7.3)
  • process.mainModule is set for the start page (such as index.html) as specified in the manifest's main field. However, when the node-main field is also specified in the manifest, process.mainModule points to the file specified in the node-main field.

global

The following names are inserted to the global object in Node's context:

  • require - this is the require() function within the main module.

require

Behaviour of relative paths in Node's require() method depends on how the parent file is used in the application (where “the parent file” is the file in which the require() method is called):

  • If the parent file was also required by Node (using require()), then the child's relative path is treated as relative to its parent.
  • If the parent file is included by WebKit (using any web technology: classic DOM window.open(), node-webkit's Window.open(), classic DOM XMLHttpRequest, jQuery's $.getScript(), HTML <script src="..."> element, etc.), then the child's relative path is treated as relative to the application's root directory.

crypto

WebKit (Chrome) has its own DOM crypto object. When you require() the Node API module of the same name (see http://nodejs.org/docs/latest/api/crypto.html for details), you have to use some other name (see issue 540 for example).

__dirname

Only Node modules (called by require() method) can use the __dirname global to read the path to their file's directory.

This global is not available in WebKit's context (including the “Developer Tools” window).

Workaround

Put exports.dirname = __dirname; in a util.js file and require it in your main JS file:

var dirname = require('./util').dirname;

console.log(dirname);
//=> /Users/johndoe/dev/project/

require('child_process')

Currently child_process.fork is broken in node-webkit works fine after 0.10.3 on Linux and 0.10.4 on Windows.

Clone this wiki locally