Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nan build error

Tags:

nan

node-gyp

I'm following the nan example,but the documention doesn't work.

my binding.gyp:

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

and my 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)

It's OK in node-gyp configure,but when node-gyp build,it reports errors:

../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());

my node version is the latest 5.7.0 and node-gyp is the latest 3.3.0 nan is latest 2.2.0. Is it possible that some code I used in the example has deprecated? Or what should I do to complete the hello example?Thanks

like image 409
caibirdme Avatar asked Feb 25 '16 03:02

caibirdme


1 Answers

I just ran into the same issue, from what I can tell the example is out dated. The following worked for me:

#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)

Basically instead of using the code from - https://github.com/nodejs/node-addon-examples/tree/master/1_hello_world I tried running the code from here - https://github.com/nodejs/node-addon-examples/tree/master/1_hello_world/nan

like image 128
AmitE Avatar answered Sep 20 '22 06:09

AmitE