官术网_书友最值得收藏!

Requirements for installing native code modules

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 to build the corresponding .node files (the .node extension is used for binary native code modules).

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.

The node-gyp tool is a cross-platform command-line tool written in Node.js for compiling native add-on 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.

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. If your system does not have the tools installed to compile native code modules, you'll see error messages. Otherwise, you'll see a node-gyp execution in the output, followed by many lines of text obviously related to compiling C/C++ files.

The node-gyp tool has prerequisites similar to those for compiling Node.js from the source—namely, a C/C++ compiler, a Python environment, and other build tools, such as Git. For Unix, macOS, and Linux systems, those are easy to come by. For Windows, you should install the following:

Normally, you don't need to worry about installing node-gyp. That's because it is installed behind the scenes as part of npm. That's done so that npm can automatically build native code modules.

Its GitHub repository contains documentation; go to 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 and of developing native code modules.

This is an example of a non-explicit dependency. It is best to explicitly declare all the things that a software package depends on. In Node.js, dependencies are declared in package.json so that the package manager (npm or yarn) can download and set up everything. But these compiler tools are set up by the OS package management system, which is outside the control of npm or yarn. Therefore, we cannot explicitly declare those dependencies.

We've just learned that Node.js supports modules written not just in JavaScript, but also in other programming languages. We've also learned how to support the installation of such modules. Next, we will learn about Node.js version numbers.

主站蜘蛛池模板: 青海省| 乐东| 昭苏县| 赤峰市| 麦盖提县| 泸水县| 通许县| 怀远县| 台江县| 常熟市| 府谷县| 扎鲁特旗| 巴塘县| 永清县| 岳阳县| 兴和县| 上思县| 北川| 阿拉善左旗| 元阳县| 保德县| 阿鲁科尔沁旗| 胶南市| 改则县| 托克逊县| 保康县| 遂溪县| 南华县| 娄烦县| 五河县| 车险| 中西区| 筠连县| 广东省| 靖江市| 五大连池市| 德惠市| 曲松县| 双桥区| 武汉市| 济南市|