簡體   English   中英

Node.js 本機模塊不是有效的 Win32 應用程序錯誤

[英]Node.js native module is not a valid Win32 application error

嘗試為 node.js 制作 Hello World 原生模塊

在 VS 2012 中使用一個文件獲得了一個 Win32 項目:

#include <node.h>
#include <v8.h>

using namespace v8;

Handle<Value> Method(const Arguments& args) {
  HandleScope scope;
  return scope.Close(String::New("world"));
}

void init(Handle<Object> target) {
  target->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)

這將編譯為 hello.node。
選項:
- 動態庫 (.dll)
- 沒有公共語言運行時支持

像這樣使用它:

hello = require './hello'
console.log hello.hello()

它適用於本地機器(win8 x64,節點:0.8.12)
但是在遠程服務器(windows server 2008 x64,節點:0.8.12,iisnode:0.1.21 x64,iis7)上它會拋出這個錯誤:

應用程序拋出了一個未捕獲的異常並被終止:錯誤:
%1 不是有效的 Win32 應用程序。

C:\\inetpub\\test\\lib\\server\\hello.node
在 Object.Module._extensions..node (module.js:485:11)
在 Module.load (module.js:356:32)
在 Function.Module._load (module.js:312:12)
在 Module.require (module.js:362:17)
在要求 (module.js:378:17)
在對象。 (C:\\inetpub\\test\\lib\\server\\index.js:32:9)
在 Module._compile (module.js:449:26)
在 Object.Module._extensions..js (module.js:467:10)
在 Module.load (module.js:356:32)
在 Function.Module._load (module.js:312:12)

我嘗試了什么:
使用應用程序池設置(啟用 win32 應用程序)沒有幫助。
iisnode x86 不能安裝在 x64 操作系統上。
由於錯誤無法編譯為 x64:錯誤 2 錯誤 LNK1112:模塊機器類型 'X86' 與目標機器類型 'x64' 沖突 C:\\derby\\hello\\build\\node.lib(node.exe) hello

有沒有人有什么建議?

我不知道是不是太晚了,但經過反復試驗,我找到了答案,主要問題(在我的機器上)是我在 windows 上編譯了 nodejs 以便能夠使用 Visual C++ 創建擴展,而我已經有了從頁面安裝了 nodejs,如果我嘗試使用默認安裝(由 nodejs 安裝程序添加到我的 PATH 中)運行測試,那么它會失敗,但是如果我使用編譯的 node.exe(我編譯為能夠引用 Visual C++ 中的庫)然后它就可以工作了。

總之,問題不在於擴展,而在於 nodejs 編譯,使用您編譯的節點(為了構建 VS 解決方案,我假設您這樣做了),然后它應該可以在遠程機器上運行。

注意:問題在於您使用的是 64 位編譯的 node.exe 來運行 32 位 dll,這就是它抱怨的原因,如果您使用 32 位的 node.exe 它應該可以工作。 (至少解決了我的問題)

只是遇到了同樣的問題,即使我的節點和插件的架構相同,我也收到了類似的錯誤消息。 事實證明,您無法重命名節點可執行文件。 它必須是node.exe ,我試圖同時測試多個版本,所以我不得不將它們放在自己的文件夾中。 之后,一切正常。

就我而言,問題是嘗試在使用 Linux 構建(適用於 Windows)的 Windows 上執行 Electron 應用程序。 我通過使用 Windows 構建它(對於 Windows)來解決。

為了在 Windows 上構建它,我使用了以下命令:

npm install --global-production windows-build-tools
npm install
npm run build:prod && electron-builder build --windows

要執行最后一個命令,您需要electron-builder ,如果沒有,請安裝它

npm install --save-dev electron-builder

使用 Electron Forge webpack typescript 樣板。 這對我有用:

webpack.main.config.js添加externals: ['sqlite3']

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: './src/electron-entrypoint.ts',
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json']
  },
  externals: ['sqlite3']
};

來源

或者 - 這也對我有用-,您可以使用better-sqlite3 ,如建議here

與您的問題無關:嘗試執行擴展名為“.node”的腳本(例如node.exe example.node )時,我遇到相同的錯誤( Error: %1 is not a valid Win32 application )。 其他擴展名(.js、.txt,根本沒有擴展名)工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM