Creating addon in nodejs

Posted By : Kamaldeep Singh | 29-Sep-2015
Using JavaScript Timing Events setInterval(); and setTimeout();

Before begining, let me give you a breif idea about some terms

  • node-gyp- is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js
  • NAN- is a thin abstraction layer between the C++ code we write and the Node and V8 APIs

Steps to create your first addon are:-

  • Run "npm init" to create package.json in your project root directory

While creating package.json specify your js file as "main": "hello.js"

  • (optional) Add a "gypfile": true entry to your package.json so that npm knows this is a binary addon that needs compiling and it needs to invoke node-gyp.
  • Install NAN node module in your project root package via "npm install nan@latest --save"
  • Create "binding.gyp" describing your source file here it is hello.cc and also define target_name defining the name of your node file i.e. hello.node. In this file, we also define our NAN module.
                    {
                        "targets": [
                            {
                                "target_name": "hello",
                                "sources": [ "hello.cc" ],
                                "include_dirs": [
                                    "<!(node -e \"require('nan')\")"
                                ]
                            }
                        ]
                    }
                
  • Write code in hello.cc to print "Hello World"
                    #include <node.h>
                    
                    namespace demo {

                        using v8::FunctionCallbackInfo;
                        using v8::Isolate;
                        using v8::Local;
                        using v8::Object;
                        using v8::String;
                        using v8::Value;
                        
                        void testMethod(const FunctionCallbackInfo<Value>& args) {
                            Isolate* isolate = args.GetIsolate();
                            args.GetReturnValue().Set(String::NewFromUtf8(isolate, "Hello World"));
                        }
                        
                        void init(Local<Object> exports) {
                            NODE_SET_METHOD(exports, "hello", testMethod);
                        }
                        
                        NODE_MODULE(addon, init)
                        
                    }
                
  • Compile your addon by running "node-gyp configure". In the ./build/ directory, a Makefile and associated property files will be created. If you don't have gyp install it via "sudo npm install node-gyp -g"
  • Next, run "node-gyp build" to start the build process

Alternatively you can use "node-gyp rebuild" to combine the configure and build

The binary output target is called "hello", we will end up with a "hello.node" file either in ./build/Release/ or ./build/Debug/ depending on how node-gyp is invoked and your environment variables.

  • Create a file called hello.js with the following contents:
                    var addon = require('./build/Release/hello.node');
                    console.log(addon.hello());
                

Here hello() is the name of funtion you have defined in NODE_SET_METHOD(exports, "hello", testMethod);

In this, the first line is responsible for loading our compiled add-on and pulling in the exports to our module.

  • To run the program, simply write "node hello.js" in your project root directory.

Finally to create your first node addon, all you need is

  • hello.cc (your C prog)
  • building.gyp (Defining targets)
  • hello.js (to run your program)

For full documentation, refer to https://nodejs.org/api/addons.html

 

THANKS

About Author

Author Image
Kamaldeep Singh

Kamaldeep is a highly skilled Backend Developer specializing in Java, specifically the Spring framework. He also has extensive knowledge of Javascript and associated frameworks such as Node.js and Express. He possesses a deep understanding of the latest technologies and has hands-on experience with Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as both relational databases like MySQL and PostgreSQL and non-relational databases like MongoDB. He has made significant contributions to various projects, including Viral Nation, ExamWorks, TNIBRO, Biogas engineering, SecureNow - Web Application, FB Messenger Chatbot, Dialogflow Chatbot, and Catalyst. Kamaldeep's expertise allows him to seamlessly integrate different technologies into applications, highlighting his adaptability and innovative mindset. His practical experience and strong technical skills make him an invaluable asset to any team.

Request for Proposal

Name is required

Comment is required

Sending message..