如何为node.js创建本机模块?

时间:2022-09-04 09:57:40

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 .

我想创建自己的本地node . js模块。我一直在跟踪http://nodejs.org/docs/latest/api/addons.html,并尝试了解如何创建本机模块。

First created hello.cc

第一次创建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

然后创建binding.gyp

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

Then I ran these command

然后我运行这些命令。

node-gyp configure
node-gyp build

finally created hello.js

最后创建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.

等等。

2 个解决方案

#1


0  

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

我尝试构建您的示例,但是由于V8版本的原因,我无法完成。

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

所以我又一次遵循了nodejs addon的例子。今天的例子和你的有点不同。和所有正确的工作。

My environment is the following:

我的环境如下:

  • SO: Ubuntu 14.04.2 LTS
  • 所以:Ubuntu 14.04.2 LTS
  • nodejs v0.12.7
  • nodejs v0.12.7
  • node-gyp v2.0.2
  • node-gyp v2.0.2
  • Python 2.7.6
  • Python 2.7.6
  • gcc 4.8.4
  • gcc 4.8.4
  • CPU Architecture: x86_64
  • CPU架构:x86_64
  • CPU op-mode(s): 32-bit, 64-bit
  • CPU op-mode(s):32位,64位
  • Byte Order: Little Endian
  • 字节顺序:小端字节序

I hope this will be useful.

我希望这是有用的。

#2


0  

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

虽然node-gyp

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

#1


0  

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

我尝试构建您的示例,但是由于V8版本的原因,我无法完成。

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

所以我又一次遵循了nodejs addon的例子。今天的例子和你的有点不同。和所有正确的工作。

My environment is the following:

我的环境如下:

  • SO: Ubuntu 14.04.2 LTS
  • 所以:Ubuntu 14.04.2 LTS
  • nodejs v0.12.7
  • nodejs v0.12.7
  • node-gyp v2.0.2
  • node-gyp v2.0.2
  • Python 2.7.6
  • Python 2.7.6
  • gcc 4.8.4
  • gcc 4.8.4
  • CPU Architecture: x86_64
  • CPU架构:x86_64
  • CPU op-mode(s): 32-bit, 64-bit
  • CPU op-mode(s):32位,64位
  • Byte Order: Little Endian
  • 字节顺序:小端字节序

I hope this will be useful.

我希望这是有用的。

#2


0  

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

虽然node-gyp

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