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:
I hope this will be useful.
I guess your example comes from following this tutorial, problem is that I get several compiler errors that seems to be due to missing imports on the current release of 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 version is
macbookproloreto:native admin$ node -v
v4.2.6
while node-gyp
is
macbookproloreto:native admin$ node-gyp -v
v3.3.1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.