Using npm to globally install nw.js-based GUI applications

  • Tutorial
Team « npm the install » in the manager package npm most often used for the local installation of the dependencies of a module made in its sub-directory «/ node_modules».

However, it is possible to run this command with the “ -g ” switch (from the word “global”). It provides the installation of the package specified by it in some global (that is, common for the whole system) place. In addition, PATH (for later launch from any desired directory) is spelled out is the team that has been specified in the property « " bin directory " » in the file package.json have installed package.

According to the documentation , such a common place under Windows is the subdirectory “/ node_modules” in the Node installation directory. In other systems, such a common place is most often the directory “/ usr / local / lib / node_modules” (whereas Node is installed there in the directory “/ usr / local / bin”).

Usually this approach is used for the global installation of various utilities designed to run from the command line. Here are some examples:

  • The npm install jshint -gcommand ensures that the “jshint” command appears on the PATH , which is used to start JSHint .
  • The npm install browserify -gcommand ensures that the “browserify” command appears on the PATH , which serves to launch Browserify .
  • Team « npm the install less See -g » provides the appearance to the PATH command «lessc», which is used to run Less.js .

However, the wedge did not converge on the CLI (on the command line) - and Habrahabr readers should already be well aware that a JavaScript application using the Node.js API can also be equipped with a GUI (graphical user interface) composed in HTML and CSS . And it will have to run this application instead of the Node on the engine, which until last year was called node-webkit , and this year (2015) was proved to be 14 January renamed in nw.js .

Your GUI applications can also be globally installed on the system from an npm package using npm. Let's talk about it.

Your first step is clear: you will certainly need to put it in an npm packagethe source code of your own GUI application, and stick it.

However, to run this code, you will also need the nw.js engine, but putting it in the same package is not very reasonable. Firstly, the volume of the engine (for Windows, which is more than 80 megabytes, for example) poses a threat of excessive load on the npm-package repository if you insert the engine into each such package. Secondly, depending on the system (Windows, Linux, Mac OS X) or on its bit capacity (32-bit or 64-bit), the engine should be different - if you put it in an npm packageall six possible versions of the engine, then not only the repository, but also the end user will start from the total volume, which reaches without a small half a gigabyte.

To overcome this problem the developers was created npm-pack nw , which is GUI-application can specify among its dependencies (see « " dependencies " » in the file package.json ) - and when this package is installed, it starts a certain in his own file package.json script a postinstall , which will automatically download from the Internet is the version nw.js,which will work for a specific end-system.

It should be noted also that the package nw determines (in the property « " bin directory " » in the file package.json ) is also a command «nw»; therefore, the GUI application can specify this command as its own "start" script , after which it will be possible to start the GUI application by issuing the " npm start " command in its directory.

But you can see right away that this is not the most convenient way to launch a GUI application.It would be much more convenient to ensure the possibility of its launch by a command consisting of one word (application name) instead of two, and, moreover, from any directory. Uzhé above it it was said that the purpose application can be achieved if its file package.json field value « " bin directory " » will be some script - then npm during the global settings are automatically put this script in your PATH, and give it the name you want. Well, the nw package exports the .findpath () method , which the script can use to find out the specific location of the downloaded nw.js engine (for subsequent launch of the engine).

For example, I’ll indicate that as part of my browserHypertext vector Fidonet open source code of such a script looks like this :

#!/usr/bin/env node
   ['.'].concat( process.argv.slice(2) ),
      cwd: __dirname,
      detached: true,
      stdio: 'ignore'

It is easy to see that there is absolutely nothing specifically fidonetovskogo in it. Therefore, you can use the same example all without the slightest changes in your own GUI applications if you intend to install them globally from an npm package.

Be sure to pay attention to the following snippets of this example:

  • Detached: trueand “ .unref () ”: the nw.js engine starts as a separate process, and the start command finishes its work immediately (does not wait for it);
  • Cwd: __dirname ”: the nw.js engine starts working in the directory where npm globally installed the start command (assuming that the entire GUI application is in the same place; but if you put this code not next to the package file .json , as I did, and in some subdirectory, then add the output from the subdirectory to the parent directory on this line);
  • ['.']. Concat (process.argv.slice (2)) ": the nw.js engine at the command line first receives a period (an instruction to take the application to be launched from the current directory), and then all the command line parameters of the start command;
  • #! / Usr / bin / env node ”: under Windows this line may not seem necessary, however this impression is illusory (actually npm relies on the presence of this line).

The above approach can also be applied to installing applications, instead of nw.js using the earlier node-webkit engine . For this purpose, instead of the npm nw package , you should use the earlier (and, moreover, unofficial) npm package - nodewebkit .

Also popular now: