P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

Creating first node.js addon

I'm following the docs to create a node.js addon. I ran

node-gyp configure build --python C:\Python27

and received error

Error: spawn ENOENT

Full stack:

gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp ERR! configure error
gyp ERR! stack Error: spawn ENOENT
gyp ERR! stack     at errnoException (child_process.js:1000:11)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:791:34)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Users\\bmackey\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" "build" "--python" "C:\\Python27"
gyp ERR! cwd D:\DevProjects\Node\AddOn
gyp ERR! node -v v0.10.29
gyp ERR! node-gyp -v v1.0.3
gyp ERR! not ok

My 3 required files are all in the same directory:

hello.cc:

// hello.cc
#include <node.h>

//Note that all node addons must export an initialization function.
//Does this go in a .h or here or what?
void Initialize (Handle<Object> exports);
NODE_MODULE(module_name, Initialize)

using namespace v8;

void Method(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope scope(isolate);
  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}

void init(Handle<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(addon, init)

binding.gyp:

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

hello.js:

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

console.log(addon.hello()); // 'world'

Upvotes: 1

Views: 157

Answers (1)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

The optional --python argument requires the path to python.exe:

node-gyp configure build --python C:\Python27\python.exe

The example on node.js website did not work for me. This guy's example worked better, but ultimately this worked and made more sense to me:

hello.cc

#include <node.h>

using namespace v8;

Handle<Value> Method(const Arguments& args) {
  HandleScope scope;
  return scope.Close(String::New("here is some output"));
}

void init(Handle<Object> target) {
  target->Set(String::NewSymbol("methodName"),
      FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(myModuleName, init)

binding.gyp:

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

hello.js:

var addon = require('./build/Release/myModuleName');

console.log(addon.methodName()); // prints "here is some output"

This layout took some of the naming ambiguity out.

Upvotes: 1

Related Questions