- Node.js Web Development
- David Herron
- 364字
- 2021-06-25 21:54:01
Native code modules and node-gyp
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 in the output a node-gyp execution, 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 source. Namely, a C/C++ compiler, a Python environment, and other build tools such as Git. For Unix/macOS/Linux systems those are easy to come by. For Windows, you should install:
- Visual Studio Build Tools: https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017
- Git for Windows: http://git-scm.com/download/win
- Python for Windows: https://www.python.org/
Normally, you won'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 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 of developing native code modules.
- SQL Server 從入門到項目實踐(超值版)
- C語言程序設計習題解析與上機指導(第4版)
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- DevOps入門與實踐
- FreeSWITCH 1.6 Cookbook
- PhpStorm Cookbook
- 概率成形編碼調制技術理論及應用
- Java編程技術與項目實戰(第2版)
- Tableau 10 Bootcamp
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- HTML5與CSS3權威指南
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- 現代C++語言核心特性解析
- Swift 2 Design Patterns