How to create native module for node.js

I want to create my own native Nodejs module. I have been following http://nodejs.org/docs/latest/api/addons.html and try to understand how to create native module .

First created hello.cc

#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const Arguments& args) {
 HandleScope scope;
 return scope.Close(String::New("world"));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("hello"),
  FunctionTemplate::New(Method)->GetFunction());
}

    NODE_MODULE(hello, init)

Then created binding.gyp

 {
   "targets": [
   {
     "target_name": "hello",
     "sources": [ "hello.cc" ]
   }
  ]
 }

Then I ran these command

node-gyp configure
node-gyp build

finally created hello.js

     var addon = require('./build/Release/hello');
      console.log(addon.hello()); 

At this time I got error like

 module.js:355
 Module._extensions[extension](this, filename);

and so on.

I tried to build your example but I don't achieve to complete because of V8 versions.

So I followed the nodejs addon example again. Today the example is a little different at yours. And all works right.

My environment is the following:

  • SO: Ubuntu 14.04.2 LTS
  • nodejs v0.12.7
  • node-gyp v2.0.2
  • Python 2.7.6
  • gcc 4.8.4
  • CPU Architecture: x86_64
  • CPU op-mode(s): 32-bit, 64-bit
  • Byte Order: Little Endian

I hope this will be useful.