首页 > 解决方案 > 需要使用电子应用程序创建节点原生模块

问题描述

我正在创建一个包含本机模块的简单电子应用程序。我已经包含了一些来自 c++ 应用程序的静态库。这是我的 binding.gyp 文件和https://nodejs.org/dist/latest/docs/api/addons.html中提到的创建的 hello.cc 文件这是我的 binding.gyp 文件

binding.gyp:

  "targets": [
    {
      "target_name": "myext",
      "libraries": [
    "/Users/rsivaram/logitech/obs-studio/deps/w32-pthreads/libpthreadGC2.a",
    "/Users/rsivaram/logitech/obs-studio/build/plugins/obs-x264/libobs-x264-util.a", 
    "/Users/rsivaram/logitech/obs-studio/build/plugins/mac-syphon/libsyphon-framework.a",
    "/Users/rsivaram/logitech/obs-studio/build/deps/file-updater/libfile-updater.a",
    "/Users/rsivaram/logitech/obs-studio/build/deps/libcaption/libcaption.a",
    "/Users/rsivaram/logitech/obs-studio/build/deps/media-playback/libmedia-playback.a",
      ],
      "cflags!": [ "-fno-exceptions" ],
      "cflags": [ "-std=c++11" ],
      "cflags_cc!": [ "-fno-exceptions" ]
        }
  ]
}

这是我的 hello.cc 文件

hello.cc:  
    #include <node.h>

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

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

void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

}  // namespace demo

“npm install”工作正常,但是我无法使用“npm start”运行应用程序。我得到的错误

1."identifier "v8" is undefined" in the hello.cc file.
2. "Module did not self-register <abs path of the app>/electron-quick-start/build/Release/myext.node "

如何创建 hello.cc 文件。请告诉我。谢谢

标签: node.jsmoduleelectronnative

解决方案


推荐阅读