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

1

6

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.

Muna answered 3/2, 2015 at 10:25 Comment(4)
"Is there any way to have console-only, GUIless, windowless application with nw.js?" Why would you do that? As opposed to just using Node?Rexanne
Adding a 40mb Webkit dependency just to use the console doesn't seem like a good idea. Can't you just ship it with the Node.js binary?Pammie
@ben-fortune, as I mentioned, there's also GUI part of that. GUI part is used by humans, console part by build system and occasionally by humans too. I'm trying to avoid shipping node.js binary along with nw.js, not vice versa.Muna
@T.J.Crowder, because there's also GUI app, and I'm trying to avoid to ship another binary as I already have one binary capable of executing JS code.Muna
H
6

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
Hospodar answered 4/2, 2015 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.