Native Node Modules, also known as Native addons, are the perfect tool for Node.js applications that need to run performance critical code, access system level APIs, or include existing C/C++ libraries. We have created the following video which will demonstrate how you can build your own Native Node.js for z/OS Module:
https://mediacenter.ibm.com/media/1_oqwm99sfIn our video, we set up the following
package.json
for our addon:
{
"name": "hello",
"version": "1.0.0",
"description": "Building your own native Node module from scratch",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"gypfile": true,
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^3.0.0"
}
}
This allows our addon to make use of N-API, the
node-addon-api
module, as well as the
bindings
module.
We set up a
binding.gyp
file where we name our addon
hello
, and add
hello.cc
as a source file:
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
}
]
}
N-API C++ exceptions are currently not support on z/OS, which is why we need to define
NAPI_DISABLE_CPP_EXCEPTIONS
so that only JavaScript exceptions get thrown.
In
hello.cc
, we create a function that simply returns the string
Hello, World!
:
#include <napi.h>
Napi::String HelloWorld(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "Hello, World!");
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::String name = Napi::String::New(env, "helloWorld");
Napi::Function function = Napi::Function::New(env, HelloWorld);
exports.Set(name, function);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
Since we named our addon
hello
, we can import it in JavaScript using:
const hello = require('bindings')('hello');
And we can call our C++ function from JavaScript using:
console.log(hello.helloWorld()); // 'Hello, World!'
If we want to allow other Node.js application to also use our addon, we also need to setup our
index.js
to export the addon:
var binding = require('bindings')('hello');
module.exports = binding;
This makes it possible to execute the command
npm install <path-to-addon>
, and
require()
our addon just like any other node module.
Congratulations, you have now built your own Native Node Module! For further information on Native Node Modules, you can check out the official Node.js documentation on
C++ addons and
N-API, as well as the
node-addon-api module's documentation.