使用“ NODE_MODULE_VERSION 70”而不是NODE_MODULE_VERSION 48

时间:2019-08-01 21:00:57

标签: javascript c++ node.js npm electron

我的电子有以下project。具有以下./src/index.js(启动我的应用程序的地方):

const { app, BrowserWindow, ipcMain } = require('electron');
const nbind = require('nbind');
const lib = nbind.init().lib;
const myPromice= new lib.MyPromice();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow();
  mainWindow.setMenu(null);
  mainWindow.maximize();

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/ui/index.html`);
  mainWindow.on('closed', () => {
    mainWindow = null;
  });

  MyPromice.exec();

};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
    app.quit();
});

app.on('activate', () => {
    createWindow();
});

我的package.json是:

{
  "name": "electron-cpp-bindings",
  "version": "1.0.0",
  "description": "A simple test using C++ bindings on an electron app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ./src/",
    "autogypi": "autogypi",
    "node-gyp": "HOME=~/.electron-gyp node-gyp --target=1.2.3 --arch=x64 --dist-url=https://electronjs.org/headers",
    "emcc-path": "emcc-path",
    "copyasm": "copyasm",
    "ndts": "ndts",
    "electron-version": "electron -v",
    "rebuild": "HOME=~/.electron-gyp electron-rebuild --version=70 --module-dir build"
  },
  "author": "Dimitrios Desyllas",
  "license": "MIT",
  "devDependencies": {
    "electron": "^5.0.8",
    "electron-rebuild": "^1.8.5"
  },
  "dependencies": {
    "autogypi": "^0.2.2",
    "nbind": "^0.3.15",
    "node-gyp": "^5.0.3"
  }
}

我的绑定由以下文件组成:

  • src/cpp/promice.h

#ifndef PROMICE
#define PROMICE

class MyPromice {
    public:
        MyPromice(){};
        ~MyPromice(){};
        void exec();
};
#endif
  • src/cpp/promice.cpp
#include"promice.h"

#include <iostream>
#include <thread>
#include <chrono>

void MyPromice::exec(){
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    std::cout << "Executing Code" << std::endl;
}

#include "nbind/nbind.h"

NBIND_CLASS(MyPromice) {
  method(exec);
}

然后我严格按照以下instructions安装要求,以便使用电子构建本机模块:

# Electron's version.
export npm_config_target=1.2.3
# The architecture of Electron, see https://electronjs.org/docs/tutorial/support#supported-platforms
# for supported architectures.
export npm_config_arch=x64
export npm_config_target_arch=x64
# Download headers for Electron.
export npm_config_disturl=https://electronjs.org/headers
# Tell node-pre-gyp that we are building for Electron.
export npm_config_runtime=electron
# Tell node-pre-gyp to build module from source code.
export npm_config_build_from_source=true
# Install all dependencies, and store cache to ~/.electron-gyp.
HOME=~/.electron-gyp npm install

然后我跑:

./node_modules/.bin/electron-rebuild

成功构建了我的模块:

✔ Rebuild Complete

另外,如果我运行以下命令序列:

npm run -- node-gyp \
  configure build

我成功构建了它:

>   configure build

> electron-cpp-bindings@1.0.0 node-gyp /home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings
> HOME=~/.electron-gyp node-gyp --target=1.2.3 --arch=x64 --dist-url=https://electronjs.org/headers "configure" "build"

make: Entering directory '/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/build'
  COPY Release/obj.target/nbind/geni/symbols.txt
  CXX(target) Release/obj.target/nbind/src/cpp/promice.o
  CXX(target) Release/obj.target/nbind/node_modules/nbind/src/common.o
  CXX(target) Release/obj.target/nbind/node_modules/nbind/src/reflect.o
  CXX(target) Release/obj.target/nbind/node_modules/nbind/src/v8/Buffer.o
  CXX(target) Release/obj.target/nbind/node_modules/nbind/src/v8/Binding.o
  SOLINK_MODULE(target) Release/obj.target/nbind.node
  COPY Release/nbind.node
make: Leaving directory '/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/build'

但是当我通过npm start启动应用程序时,出现以下错误:

> electron-cpp-bindings@1.0.0 start /home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings
> electron ./src/

App threw an error during load
Error: The module '/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/build/Release/nbind.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 48. This version of Node.js requires
NODE_MODULE_VERSION 70. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (electron/js2c/asar.js:155:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:881:18)
    at Object.func [as .node] (electron/js2c/asar.js:155:31)
    at Module.load (internal/modules/cjs/loader.js:701:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:633:12)
    at Function.Module._load (internal/modules/cjs/loader.js:625:3)
    at Module.require (internal/modules/cjs/loader.js:739:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at initNode (/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/node_modules/nbind/dist/nbind.js:141:15)
    at /home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/node_modules/nbind/dist/nbind.js:115:13
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The module '/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/build/Release/nbind.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 48. This version of Node.js requires
NODE_MODULE_VERSION 70. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (electron/js2c/asar.js:155:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:881:18)
    at Object.func [as .node] (electron/js2c/asar.js:155:31)
    at Module.load (internal/modules/cjs/loader.js:701:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:633:12)
    at Function.Module._load (internal/modules/cjs/loader.js:625:3)
    at Module.require (internal/modules/cjs/loader.js:739:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at initNode (/home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/node_modules/nbind/dist/nbind.js:141:15)
    at /home/pc_magas/Kwdikas/Javascript/electron-cpp-bindings/node_modules/nbind/dist/nbind.js:115:13

也为了解决我的问题,我还运行以下命令序列:

rm -rf ./node_mobules
npm install

我的路无光。

所以我想知道为什么我的nbind标头配置错误,以及如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我有同样的问题。在尝试了许多版本的代码后,我发现了这一点:

  1. 在JSON文件中添加脚本(您可以更改此行)

    “ rebuild”:“电子重建-f -w node-gyp”

  2. 构建c ++文件

node-gyp configure build

  1. 现在制作相同的版本

npm run rebuild

  1. 现在运行电子

    npm开始

正在工作!试试看。 我不太了解,但是我发现node.js和node-gyp运行不同版本的node,因为它避免了冲突。