📜 ⬆️ ⬇️

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

The “ npm install ” command in the npm package manager is most often used to locally install the dependencies of a module performed in its / node_modules subdirectory .

However, it is possible to run this command with the key “ -g ” (from the word “global”). It ensures that the package specified to it is installed in a global (that is, common to the entire system) place. In addition, in the PATH (for the subsequent launch from any desired directory), the prescribed command appears that was specified in the "bin" property in the package.json file of the installed package.

This common place, according to the documentation , under Windows is the “/ node_modules” subdirectory in the Node installation directory. In other systems, the most common place is the “/ usr / local / lib / node_modules” directory (whereas Node is installed there in the “/ usr / local / bin” directory).
')
Usually this approach is used for global installation of various utilities designed to be run from the command line. Here are some examples:


However, on the CLI (on the command line) the light did not come together - and Habrahabr readers should be completely aware that a JavaScript application using the Node.js API can also be equipped with a GUI (graphical user interface) written in HTML and CSS . To do this, you will have to launch such an application instead of Node on that engine, which until last year was called node-webkit , and this year (2015) turned out to be renamed to nw.js on January 14th .

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 the source code of your GUI application itself into the npm package , and stick it.

However, to run this code, you will also need the nw.js engine, but to put 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) creates the threat of excessive load on the repository of npm-packages, if you put the engine in each such package. Secondly, depending on the system (Windows, Linux, Mac OS X) or its bit depth (32-bit or 64-bit), the engine should be different - if you shove all six possible versions of the engine into the npm package , then not only the repository, but also the end user will start from the total volume, reaching without a small half a gigabyte.

To overcome this problem, the developers created the nw np package , which the GUI application can specify among its dependencies (in the "dependencies" section of the package.json file) —and when this package is installed, it will start running in its own the package.json file is a postinstall script that automatically downloads exactly the version of nw.js from the Internet that will fit for a specific final operating system.

It should be noted also that the nw package (in the “bin” property in the package.json file) also defines the “nw” command; therefore, a 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 it is immediately apparent that this is not the most convenient way to launch a GUI application. It would be more convenient to ensure that it can be launched by a command consisting of one word (application name) instead of two, and, moreover, from any directory. Above, it was said that an application can achieve this goal if in its package.json file the value of the "bin" field is some script - then npm during global installation automatically puts this script in the PATH and gives it the desired name. Well, the nw package exports the .findpath () ” method, which the script can use when figuring out the specific location of the downloaded nw.js engine (for the subsequent launch of the engine).

For example, I’ll point out that, as part of my browser of hypertext vector Fidonet, the open source code of such a script looks like this :

#!/usr/bin/env node require('child_process').spawn( require('nw').findpath(), ['.'].concat( process.argv.slice(2) ), { cwd: __dirname, detached: true, stdio: 'ignore' } ).unref(); 

It is not difficult to see that there is absolutely nothing specifically Fidonet's in it. Therefore, you can use the same example without any changes in your own GUI applications, if you intend to install them globally from the npm package.

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


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

Source: https://habr.com/ru/post/252029/


All Articles