Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to build console application with nw.js?

Is there any way to have console-only, GUIless, windowless application with nw.js?

We have some internal utility originally made with App.js and recently converted to nw.js (formerly node-webkit). This tool consists of GUI app and console-only (integrated to build process) counterpart. App.js was able to execute any *.js like nodejs, in console, but in nw.js it seems mandatory for application to have some main *.html and window. Even when with node-main in manifest it requires main field to be present as well.

like image 967
k12th Avatar asked Feb 03 '15 10:02

k12th


People also ask

Why use nw js?

NW. js gives you the flexibility to freely choose whatever tools you'd like to work with. It's truly great to be able to create hybrid applications by focusing only on the web implementation you're already familiar with. Of course, NW.

How does nw js work?

NW. js makes Node. js available through the context of the embedded web browser, which means you can script JavaScript files that access both Node. js' API, as well as API methods related to the browser's JavaScript namespace, such as the WebSocket class.

Does NW js use chromium?

NW. js uses a compiled version of Chromium with custom bindings, whereas Electron uses an API in Chromium to integrate Node.


1 Answers

Yes, just add "show": false in package.json

{
  "name": "My CLI App",
  "main": "index.html",
  "window": {
    "show": false
  }
}

Docs for package.json options

If you want you can make app.nw package which will be open with node-webkit, so you don't need to ship big package.


You also can make a wrapper to run simple .js files from terminal:

#!/bin/bash
# file nw-runner
BASEDIR=$(dirname $0)
/Applications/node-webkit.app/Contents/MacOS/node-webkit $BASEDIR/path/wrapper_app "$@"

So path/wrapper_app will contain our application (package.json, index.html) and we will require specified file:

var args = require('gui').App.argv;
var path = require('path'), fs = require('fs');
var runable = path.relative(process.env.PWD, args[0]);

if (fs.existsSync(runable)) {
  require(runable);
} else {
  process.stdout.write("Can not not find file " + args[0]);
  process.exit(1);
}

Then can run *.js file like this:

nw-runner ./my_app.js
like image 156
Pavel Evstigneev Avatar answered Oct 17 '22 16:10

Pavel Evstigneev