gpt4 book ai didi

javascript - 使用 `NODE_MODULE_VERSION 70.` 而不是 NODE_MODULE_VERSION 48 重建 nbind

转载 作者:行者123 更新时间:2023-12-03 12:43:17 27 4
gpt4 key购买 nike

我有以下 project与 Electron 。有以下./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"
}
}


我的绑定(bind)由以下文件组成:
  • 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 构建 native 模块:
    # 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 配置了错误的 header ,以及如何解决这个问题?

    最佳答案

    我遇到过同样的问题。在尝试了许多版本的代码后,我发现了这个:

  • 在 JSON 文件中添加脚本(在您的情况下更改此行)
    “重建”:“Electron 重建 -f -w Node gyp”
  • 构建 c++ 文件
  • node-gyp configure build
  • 现在制作相同的版本
  • npm run rebuild
  • 现在运行 Electron
    npm 开始

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

    关于javascript - 使用 `NODE_MODULE_VERSION 70.` 而不是 NODE_MODULE_VERSION 48 重建 nbind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316922/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com