How to create your own module for node.js

I want to create my own custom Nodejs module. I am following http://nodejs.org/docs/latest/api/addons.html and trying to figure out how to create my own module.

Hello.cc created first

#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 bind.gyp

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

Then I ran this 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 an error, for example

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

etc.

+4
source share
2 answers

I tried to create your example, but I did not achieve completion due to V8 versions.

So, I repeated the addjs node example again . Today this example is slightly different from yours. And everything is working correctly.

My surroundings are as follows:

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

, .

0

, , , , , , node:

../hello.cc:7:15: error: calling a protected constructor of class 'v8::HandleScope'
  HandleScope scope;
              ^
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:885:13: note: declared protected here
  V8_INLINE HandleScope() {}
            ^
../hello.cc:8:16: error: no member named 'Close' in 'v8::HandleScope'
  return scope.Close(String::New("world"));
         ~~~~~ ^
../hello.cc:8:30: error: no member named 'New' in 'v8::String'
  return scope.Close(String::New("world"));
                     ~~~~~~~~^
../hello.cc:13:29: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Handle<v8::Value> (const v8::internal::Arguments &)'
      FunctionTemplate::New(Method)->GetFunction());
                            ^~~~~~
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:4350:16: note: passing argument to parameter 'isolate' here
      Isolate* isolate, FunctionCallback callback = 0,
               ^
../hello.cc:12:23: error: no member named 'NewSymbol' in 'v8::String'
  target->Set(String::NewSymbol("hello"),
              ~~~~~~~~^
In file included from ../hello.cc:1:
In file included from /Users/admin/.node-gyp/4.2.6/include/node/node.h:42:
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:327:9: error: cannot initialize a member subobject of type 'v8::FunctionTemplate *' with an lvalue of type 'const char *'
      : val_(that) {}
        ^    ~~~~
../hello.cc:8:34: note: in instantiation of function template specialization 'v8::Local<v8::FunctionTemplate>::Local<const char>' requested here
  return scope.Close(String::New("world"));

Node

macbookproloreto:native admin$ node -v
v4.2.6

node-gyp -

macbookproloreto:native admin$ node-gyp -v
v3.3.1
0

All Articles