Summary - Native addons
- Node.js course
- Expert
- Lesson 12/47
- 5 min
Combine N-API, binding and build to write a native module.
Summary - Native addons
In this group we saw the core pieces of native addons: N-API, node-addon-api, binding, callback and build. Each handles one aspect of connecting C/C++ code to Node.js.
N-API provides a version-stable interface. node-addon-api offers a C++ wrapper. Binding connects a JS function to native code. Callback carries a response from native to JS. Build (node-gyp) compiles the source and produces a .node file. In the final lesson we combine them in a report.
The report function
The final task combines all addon pieces into one report. The addonReport() function joins mod, wrapper and binding info with ' | '. Output: N-API v9 | node-addon-api | add.
This report summarizes three core aspects of a native addon: the API (N-API v9), the wrapper (node-addon-api) and the bound function (add). Inside the function you place each piece in a variable, then join them with +. This lets you see what you learned on the console; you model the structure without compiling a real addon.
Practice Write the addon report
Write function addonReport(). Define const mod = {api: 'N-API', version: 9}. Define const wrapper = 'node-addon-api'. Define const binding = {name: 'add', arity: 2}. Return mod.api + ' v' + mod.version + ' | ' + wrapper + ' | ' + binding.name. Print console.log(addonReport()).
Starter code
// Write an addonReport function
// Combine api, wrapper and binding info
// Return 'N-API v9 | node-addon-api | add'
// Call and print
// write hereSample solution
function addonReport() {
const mod = {api: 'N-API', version: 9};
const wrapper = 'node-addon-api';
const binding = {name: 'add', arity: 2};
return mod.api + ' v' + mod.version + ' | ' + wrapper + ' | ' + binding.name;
}
console.log(addonReport());Expected output
N-API v9 | node-addon-api | add
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which tool compiles the addon?
- npm
- node-gyp Correct answer
- require
- express
Explanation: node-gyp compiles the addon with binding.gyp.
2 Match each concept to its role
| Term | Meaning |
|---|---|
N-API |
Stable interface |
node-addon-api |
C++ wrapper |
Binding |
JS-native bridge |
node-gyp |
Build tool |