javascript - 创建第一个 node.js 插件

标签 javascript c++ node.js

我正在关注 docs创建一个 node.js 插件。我跑了

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

收到错误

Error: spawn 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 个文件都在同一个目录中:

你好.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)

绑定(bind).gyp:

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

你好.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 网站上的示例对我不起作用。 This guy's这个例子效果更好,但最终这个效果对我来说更有意义:

你好

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

绑定(bind).gyp:

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

你好.js:

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

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

这种布局消除了一些命名歧义。

关于javascript - 创建第一个 node.js 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29018664/

相关文章:

node.js - 模块别名包的智能感知

c - 使用 Node.js ffi 模块分配无符号字符的缓冲区

javascript - 如何在javascript中找到两条切线的交点

javascript - 如何在 javascript 的禁用按钮上启用工具提示?

javascript - 谁能解释为什么 focus() 不总是在 IE 10 上工作?

javascript - 从 Javascript 调用 Typescript 类时的语法

c++ - 使用 GCC 在可执行文件中嵌入资源

c++ - 使用 AWS C++ SDK 分段上传 S3

c++ - 使用大型源代码文件的 IDE 建议

javascript - 使用 google-passport-oauth 登录后 req.user 未定义