簡體   English   中英

創建第一個node.js插件

[英]Creating first node.js addon

我正在按照文檔創建一個node.js插件。 我跑了

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

並收到錯誤

錯誤:產生ENOENT

全棧:

gyp info it worked if it ends with ok
gyp info using node-gyp@1.0.3
gyp info using node@0.10.29 | 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

我的3個必需文件都在同一目錄中:

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'

可選的--python參數需要python.exe的路徑:

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

node.js網站上的示例不適用於我。 這個人的例子工作得更好,但最終這對我來說更有意義:

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"

這種布局消除了一些命名歧義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM