
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:
- The “ npm install jshint -g ” command provides the appearance of the “jshint” command in the PATH , which serves to launch JSHint .
- The “ npm install browserify -g ” command provides the appearance of the “browserify” command in the PATH , which serves to launch Browserify .
- The “ npm install less -g ” command ensures the appearance of the “lessc” command in PATH , which serves to start Less.js.

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:
- “ Detached: true ” and “ .unref () ”: the nw.js engine starts as a separate process, and the start command ends its work immediately (does not wait for it);
- “ Cwd: __dirname ”: the nw.js engine starts in the directory where npm globally installed the start command (assuming that the entire GUI application is in the same place; but if this code was not put next to the package file .json , as I did, and in some subdirectory, add the output from the subdirectory to the parent directory in this line);
- “ ['.']. Concat (process.argv.slice (2)) ”: the nw.js engine in the command line first sends a point (an instruction to take the application being started 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, but this impression is illusory (in fact, npm relies on the presence of this line).
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 .