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.
source
share