2015-10-07 14 views
5

我完全不熟悉Node JS和Electron。我正在嘗試使用Electron和Node JS將C++與HTML集成。我已經通過一些例子給出了:GIT電子調用節點本機擴展(C++)

我想要做的是從我的網頁的JavaScript電子加載調用本機函數(你好())。我使用node-gyp configure來生成我的Visual Studio解決方案文件。 (.sln)。後來我用Visual Studio 2013 Express編譯了我的代碼,它在build \ Release文件夾中成功生成了我的.node文件。

這是我的index.js文件:

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

時,我只需用node index.js運行它,它給我所需要的輸出:

world 

但問題帶有當我使用電子。我使用電子二進制(32位)來運行我的網頁。

以下是我main.js文件:

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; 
    }); 
}); 

現在,這是我的JavaScript在那裏我調用本地插件:

//************* My Functional logic ************** 
//************************************************ 

var addon = require('../build/Release/hello'); 
alert(addon.hello()); 

當我運行這個或加載此我收到以下錯誤:

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

正在關注是我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 
} 

這是我binding.gyp

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

我使用'--arch = ia32'標誌時運行到同一個問題來設置當我使用' - arch = x64' flag收到'Uncaught Error:系統無法在%2的消息文件中找到消息號爲0x%1的消息文本。'但是,如果我用'node。/'運行該命令,則會打印出「world 「到預期的命令行,同樣的代碼在電子MacOS上工作,所以似乎是一個基於Windows電子的問題 – devonbleibtrey

回答

2

看起來你可能沒有配置正確的二進制文件。對不起不知道這是否會在本機模塊的工作,但你可以嘗試rebuilding ...

注:請確保你有你的節點GYP命令正確的參數(如果這是你將如何重建) 。

  • --target=<your electron version>
  • --target_platform=win32(本示例中不鏈接,但你似乎是使用Windows)
  • --arch=<your architecture>(64 = 64位,86 = 32位)
+0

我在我的開發環境中沒有找到任何這樣的目錄結構我只使用電子二進制運行我的應用程序 – user1636077

+0

我的不好,我不應該在凌晨3點嘗試閱讀問題,我已經更新了我的答案。 – Josh

2

正如我所提到的評論我遇到了同樣的問題。要解決它,你需要添加一對附加標誌:

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

這將讓來自atom.io網站的正確要求,並建立正確的加載項。欲瞭解更多信息,你可以檢查電子的具體docs使用本地模塊。

0

直接使用node-gyp將使用nodejs標頭構建,但電子標頭不同。

首先你應該弄清楚女巫電子版電子正在使用。 你可以寫一個js這樣

console.log(process.version); 

使用電子執行該腳本,我的版本是0.36.1 並將目錄更改爲您想建立有關

#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 
1

我個人的問題模塊節點VS在本地插件電子頭需要不同的dist-URL參數值:

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

希望它會幫助別人。

PS:還是無法弄清楚如何使用.npmrc在窗口(

+0

把它放在你的.npmrc中在您的項目根文件夾中 'runtime = electron \ n target_arch = x64/x86 \ n target = 1.7.9 \ n disturl = https://atom.io/download/atom- shell \ n progress = true''' – niketan

+0

@niketan,但是接下來我將如何構建替代架構?每次更改文件? – Konstantin