4

I'm trying to integrate C++ with HTML using Electron and Node JS.But I'm confused by the NODE_MODULE_VERSION. I have a binding.gyp like examples.

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

and a 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"));
}

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

NODE_MODULE(addon, init)

}  // namespace demo

and after node-gyp configure build,they run well with node

const addon = require('./package/build/Release/addon');
addon.hello();

But when I combined it with electron,there is something wrong. enter image description here

I have read the APIs of both electron and nodejs.Some told me to install nvm to change nodejs's version,and others advice me to install electron-build.I tried,but they don't work.(I think it will be strange if they work). I think what's the matter is the version of nodejs.But my node is v7.7.4!So,how can i update the version of modules... thanks~

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Zhi Zhang
  • 101
  • 2
  • 6
  • It's the first time for me to ask question...seems like there are a lot of problems... – Zhi Zhang Mar 24 '17 at 10:22
  • There doesnt seem to be anything wrong with the question.. formatting and editing revisions happen all the time here – Suraj Rao Mar 24 '17 at 10:25
  • Really?I'm afrid of that masters can't understand me... – Zhi Zhang Mar 24 '17 at 10:36
  • as long as the question follows [guidelines](http://stackoverflow.com/help/how-to-ask) it will do fine – Suraj Rao Mar 24 '17 at 10:39
  • how do you build it with `node-gyp`? you should follow electron guide: https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md#manually-building-for-electron – pergy Mar 28 '17 at 08:55

2 Answers2

3

As @pergy mentioned you should build with --taget option pointing to the correct iojs(node) version used in electron.

see a working sample(windows) here. https://github.com/weliwita/electron-edge-sample/tree/42996881/node-addons-c-in-electron

rawel
  • 2,923
  • 21
  • 33
-1

I have posted a git project here:https://github.com/anurag2911/angular-electron-nodeaddon This is a simple angular-electron-nodeaddon project.This is a working example of how to write c++ node addons and use them in electron.

Anurag Kumar
  • 9
  • 1
  • 6