Call Node Native Addons (C ++) in Electron

I am completely new to Node JS and Electron. I am trying to integrate C ++ with HTML using Electron and Node JS. I gave some examples below: GIT

What I'm trying to do is call the native (hello ()) function from my javascript webpage, which is electronically loaded. I used node-gyp configure to create my Visual Studio solution file. ( .sln ). And later, I compiled my code using Visual Studio 2013 Express, which successfully generated my .node file in the \ Release Folder assembly.

This is my index.js file:

 var addon = require('./build/Release/hello.node'); console.log(addon.hello()); 

when I just run this using node index.js it gives me the desired result:

 world 

But the problem arises when I use Electron. I am using an electronic binary (32 bit) to launch my web page.

Below is my main.js file:

 var app = require('app'); // Module to control application life. var BrowserWindow = require('browser-window'); // Module to create native browser window. require('crash-reporter').start(); var mainWindow = null; // Quit when all windows are closed. app.on('window-all-closed', function() { if (process.platform != 'darwin') { app.quit(); } }); // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on('ready', function() { mainWindow = new BrowserWindow({width: 1366, height: 768}); mainWindow.loadUrl("file://" + __dirname + "/HtmlFile/index.html"); mainWindow.on('closed', function() { mainWindow = null; }); }); 

Now this is my javascript where I call my own addon:

 //************* My Functional logic ************** //************************************************ var addon = require('../build/Release/hello'); alert(addon.hello()); 

When I run this or load this page, I get the following error:

 Uncaught Error: %1 is not a valid Win32 application. ATOM_SHELL_ASAR.js:137 C:\Users\Administrator\Desktop\MyAPP\build\Release\hello.node 

Below is my package.json :

 { "name": "MyAPP", "version": "1.0.0", "description": "Desc", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "nan": "^2.0.9" }, "gypfile": true } 

This is my binding.gyp :

 { "targets": [ { "target_name": "hello", "sources": [ "hello.cc" ], "include_dirs": [ "<!(node -e \"require('nan')\")" ] } ] } 
+5
source share
4 answers

It looks like you cannot have the correct binary file configured. Sorry, not sure if this will work for your own module, but you can try rebuilding ...

Note. Please make sure that you have the correct arguments for your node -gyp command (if so, how you will rebuild).

  • --target=<your electron version>
  • --target_platform=win32 (Not in the example link, but you seem to be using windows)
  • --arch=<your architecture> (x64 = 64 bits, x86 = 32 bits)
+2
source

As I mentioned in the comments, I ran into this problem. To solve this problem, you need to add a couple more flags:

 node-gyp rebuild --target=<your electron version> --arch=<insert your arch> --dist-url=https://atom.io/download/atom-shell 

This will allow you to get the correct requirements from atom.io and build the add-in correctly. For more information, you can check the electronic specifics of docs using your own modules.

+2
source

My personal problem regarding node versus electronic headers in native add-ons required a different dist-url parameter value:

  --dist-url=https://atom.io/download/electron 

Hope this helps someone.

PS: I still can’t figure out how to install it using .npmrc in windows (

+1
source

using node -gyp will directly build headers with nodejs, but the electron has different headers.

You must first understand that the electronic electronic version of the electronic version is being used. you can write js like this

 console.log(process.version); 

use the electron to execute this script, my version is 0.36.1 and change the directory to the module you want to build

 #On Windows Try this cd /path-to-module/ npm install bindings nan node-gyp node-gyp rebuild --target=0.36.1 --arch=x64 --dist- url=https://atom.io/download/atom-shell #notice the target version is the electron binary version 
0
source

Source: https://habr.com/ru/post/1233121/


All Articles