Nan构建错误

时间:2016-02-25 03:03:43

标签: nan node-gyp

我跟随纳米示例,但documention没有效果。

我的binding.gyp:

{
  "targets":[
    {
      "target_name": "hello",
      "sources": ["hello.cpp"],
      "include_dirs": [
        "<!(node -e \"require('nan')\")"
      ]
    }
  ]
}

和我的hello.cpp:

#include <nan.h>

using namespace v8;

NAN_METHOD(Method) {
    NanScope();
    NanReturenValue(String::New("world"));
}

void Init(Handle<Object> exports) {
    exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());
}

NODE_MODULE(hello, Init)

node-gyp configure中可以,但在node-gyp build时,它会报告错误:

../hello.cpp:10:9: error: use of undeclared identifier 'NanScope'
    NanScope();
    ^
../hello.cpp:11:33: error: no member named 'New' in 'v8::String'
    NanReturenValue(String::New("world"));
                    ~~~~~~~~^
../hello.cpp:15:18: error: use of undeclared identifier 'NanSymbol'
exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());
             ^
../hello.cpp:15:60: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Nan::NAN_METHOD_RETURN_TYPE (Nan::NAN_METHOD_ARGS_TYPE)'
exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());

我的节点版本是最新的5.7.0,node-gyp是最新的3.3.0 nan是最新的2.2.0。 我在示例中使用的某些代码是否可能已弃用? 或者我该怎么做才能完成你好的例子?谢谢

1 个答案:

答案 0 :(得分:2)

我刚刚遇到了同样的问题,我可以说这个例子已经过时了。 以下对我有用:

#include <nan.h>

void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  info.GetReturnValue().Set(Nan::New("world").ToLocalChecked());
}

void Init(v8::Local<v8::Object> exports) {
  exports->Set(Nan::New("hello").ToLocalChecked(),
               Nan::New<v8::FunctionTemplate>(Method)->GetFunction());
}

NODE_MODULE(hello, Init)

基本上不使用 - https://github.com/nodejs/node-addon-examples/tree/master/1_hello_world中的代码 我尝试从这里运行代码 - https://github.com/nodejs/node-addon-examples/tree/master/1_hello_world/nan