Installing the prepackaged Node.js distributions is currently the preferred installation method. However, installing Node.js from source is desirable in a few situations:
Now that you have the high-level view, let's get our hands dirty mucking around in some build scripts. The general process follows the usual configure
, make
, and make install
routine that you may already have performed with other open source software packages. If not, don't worry, we'll guide you through the process.
The official installation instructions are in the README contained within the source distribution at https://github.com/nodejs/node/blob/master/README.md.
As noted in the preceding section, there are three prerequisites: a C compiler, Python, and the OpenSSL libraries. The Node.js installation process checks for their presence and will fail if the C compiler or Python is not present. The specific method of installing these is dependent on your operating system
These commands will check for their presence:
$ cc --version Apple LLVM version 7.0.2 (clang-700.1.81) Target: x86_64-apple-darwin15.3.0 Thread model: posix $ python Python 2.7.11 (default, Jan 8 2016, 22:23:13) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
While we won't discuss native code module development in this book, we do need to make sure that they can be built. Some modules in the NPM repository are native code, and they must be compiled with a C or C++ compiler, and build the requisite .node
files.
The module will often describe itself as a wrapper for some other library. For example, the libxslt
and libxmljs
modules are wrappers around the C/C++ libraries of the same name. The module includes the C/C++ source code, and when installed, a script is automatically run to do the compilation with node-gyp
.
You can easily see this in action by running these commands:
$ mkdir temp $ cd temp $ npm install libxmljs libxslt
This is done in a temporary directory, so you can delete it afterward. You'll see in the output a node-gyp
execution, followed by many lines of text obviously related to compiling C/C++ files.
The node-gyp
tool is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. We've mentioned native code modules several times, and it is this tool that compiles them for use with Node.js.
Normally, you won't need to worry about installing node-gyp
. That's because it is installed behind the scenes as part of NPM. It's done so that NPM can automatically build native code modules.
Its GitHub repository contains documentation at https://github.com/nodejs/node-gyp.
Reading the node-gyp
documentation, in its repository, will give you a clearer understanding of the compilation prerequisites discussed previously, as well as developing native code modules.