如何为node.js创建本地模块

4
我希望创建自己的本地 Nodejs 模块。我一直在关注 http://nodejs.org/docs/latest/api/addons.html 并尝试理解如何创建本地模块。
首先创建了 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)

然后创建了binding.gyp。
 {
   "targets": [
   {
     "target_name": "hello",
     "sources": [ "hello.cc" ]
   }
  ]
 }

然后我运行了这些命令。
node-gyp configure
node-gyp build

最终创建了hello.js

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

此时我收到了如下错误

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

等等。


我还没有尝试过,但你读过这篇教程吗?(http://code.tutsplus.com/tutorials/writing-nodejs-addons--cms-21771) - MaxArt
不用了,我会仔细阅读的。谢谢。 - GPrathap
你发布的错误信息只包含了出错的那一行,但并没有包含错误信息本身,请更新一下。 - loganfsmyth
2个回答

0
我猜你的例子来自于以下this教程,问题是我得到了几个编译器错误,似乎是由于当前版本的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

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

所以我再次按照 nodejs addon example 进行。今天的示例与您的略有不同。而且一切正常。

我的环境如下:

  • 操作系统:Ubuntu 14.04.2 LTS
  • nodejs v0.12.7
  • node-gyp v2.0.2
  • Python 2.7.6
  • gcc 4.8.4
  • CPU架构:x86_64
  • CPU操作模式(s):32位、64位
  • 字节顺序:小端

希望这对您有用。


谢谢。我会去查看的。 - GPrathap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接