node-js-banner

CREATING NATIVE EXTENSION CODES FOR NODE.JS

Introduction

Among the javascript functionalities, node.js plays the most vital role in the developing environment as well as from the user perspective. This article depicts the high necessity concepts for developing native extensions for Node.js. This write up will be explaining to you about building the first native extensions in a detailed way. So without wasting much time here let’s get directly started with the explanation. As the best website development company in Chennai, iStudio Technologies always deliver quality website for their clients using updated web app frameworks.

Brushing up with basics

To make it simple and straight the native extensions used in node.js functionalities are purely based on C++ that is implemented with logic and can be appealed from JavaScript code later. It’s highly important and interesting at the same time in knowing the working of NodeJS and the essential parts involved in it. Hence it is highly necessary to know the reason behind discussing JavaScript and C++ languages under the shelter of NodeJS at this instance.

Let me explain in this way:

  • The javascript is the coding language.
  • V8 is the engine that runs on JavaScript code.
  • Libuv as a C library supports us through providing asynchronous execution.
node-js-ban-image

Now it’s time to find the spot for fixing the native extensions and let us go with read/write disk action to explain it. But the scenario is both JavaScript as well as V8 can’t provide us with disk access. Nodejs has the capability to read/write the disk and we also have Libuvwhich provides us with an asynchronous execution. This is the exact point were native extensions happen and get in touch. Post establishing native extensions the fs module is implemented using C++ and obviously gets exposed to certain methods where it can be invoked using JavaScript. These are the basics to learn about node.js and native extensions in order to implement the same. As the best web development company in Chennai, iStudio Technologies provides the agilest website development service for their diversified set of clients.

Tools integrated for creating native extensions

a) Binding.GYP File

This specific file demands us to specify the need to compile the native extension. The most important thing to be noticed here is that it is highly mandatory to give proper justification for the files that are compiled here and the final binary which is been called in the functionality. This one has a JSON like structure in it and the keys to get this highly configured by its sources and target as well.

b) NODE-GYP

This one is basically a tool that helps us to get compiled with the native extensions. This tool is implemented in Node.js which is been packaged with the npm, so we can just run npm, install it, and then compile it using our native extensions. While running the npm installation, it will help by detecting the binding.gyp file gets included in the root folder and starts to get compiled. Moreover, it allows us to release or debug builds. This will yield the result of binary files get combined with the specific .node extensions that will be created within the release or debug folder, based on the way it got configured.

c) Bindings

Bindings are a node.js package that allows us to export the native extensions of node.js. It takes the charge of searching in a built or release folder for development purpose.

d) N-API

A C API that helps us to get interacted with the engine entirely in the abstract way. It’s almost an evolution kind of approach that tries its best to port its nodes in various architectures. This API provides high stability and greater compatibility between different node versions. It means that if the native extension is compiled to node 8.1, then there is no necessity to recompile it for the versions 8.6 and 9.3. This makes the job of maintainers and contributors much easier. As a leading website development company in Chennai, iStudio Technologies incorporates the most advanced frameworks in order to deliver the best website for your concern.

e) Node ADDON API

Taking advantage of the language used this particular module provides us the implementation of C++ for N-API.

Step1: Native Extension World

To start with the native extension world we can just use the example of a simple demo program here. There is no intention of overloading the code with extra logic in order to focus on the minimum necessary code.
Here we can start the implementation through initializing the npm where the dependency factors are installed:

npm init

Time to install our dependencies

npm i node-addon-api bindings
Parts of the file

e) Node ADDON API

The first argument is the native extension name and the second argument is the name of the function which initializes the extension.

Init:

This specific function is used to start the native extension of node.js where the functions are exported which can be invoked from the JavaScript code. In order to carry out this task we need to set the name of the function to the export objects and the function can be invoked as well. This function must return the export object at any case.
. As the best website development company in Chennai, iStudio Technologies provides industry-specific web development service to their diversified clients.

SayHi:

This particular function is something which gets executed once the native extensions from JavaScript are invoked.

Other additional steps

  • NAPI
  • Node-addon-api
  • Nan (Native abstract for node.js)

Things existed before in N-API

It is very important to know about the context and history of native extensions as it will be very helpful to give access to a lot of documentation and examples as well. The basic motto is to replace the NAN with N-API, for that reason we need turn back and have a look at NAN. As a C++ library NAN delivers us v8 abstraction, but at the same time, it won’t allow to abstract ourselves the v8. While releasing NodeJS it tends to undergo v8 related changes which can even break the native extension. Usage if NAN is considered to be a remedy to avoid this problem.

Conclusion

As a developer if you want to know the working process and composition of NodeJS then it is essential to learn about native extensions here. It is a multi-usage pack which can be used in different scenarios like performance boost, C/C++ library integrations or legacy code integration. We at iStudio Technologies are the top web development company in Chennai, having a decade of experience and known as the best client servicing company in the industry.