Node.js 本机模块不是有效的 Win32 应用程序错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13035760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js native module is not a valid Win32 application error
提问by Vladimir Makhaev
Trying to make Hello World native module for node.js
尝试为 node.js 制作 Hello World 原生模块
Got an Win32 Project in VS 2012 with one file:
在 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)
That`s compiles to hello.node.
Options:
- Dynamic Library (.dll)
- No Common Language Runtime Support
这将编译为 hello.node。
选项:
- 动态库 (.dll)
- 无公共语言运行时支持
Use it like:
像这样使用它:
hello = require './hello'
console.log hello.hello()
It works on local machine (win8 x64, node: 0.8.12)
But on remote server (windows server 2008 x64, node: 0.8.12, iisnode: 0.1.21 x64, iis7) it throws this error:
它适用于本地机器 (win8 x64, node: 0.8.12)
但在远程服务器 (windows server 2008 x64, node: 0.8.12, iisnode: 0.1.21 x64, iis7) 上,它抛出此错误:
Application has thrown an uncaught exception and is terminated: Error:
%1 is not a valid Win32 application.C:\inetpub\test\lib\server\hello.node
at Object.Module._extensions..node (module.js:485:11)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object. (C:\inetpub\test\lib\server\index.js:32:9)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
应用程序抛出了一个未捕获的异常并被终止:错误:
%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)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object。(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)
What I tryed:
Playing with app pool settings (enable win32 apps) does not helped.
Iisnode x86 does not install on x64 os.
Can`t compile to x64 because of error: Error 2 error LNK1112: module machine type 'X86' conflicts with target machine type 'x64' C:\derby\hello\build\node.lib(node.exe) hello
我尝试过的:
使用应用程序池设置(启用 win32 应用程序)没有帮助。
iisnode x86 不能安装在 x64 操作系统上。
由于错误无法编译为 x64:错误 2 错误 LNK1112:模块机器类型 'X86' 与目标机器类型 'x64' 冲突 C:\derby\hello\build\node.lib(node.exe) hello
Does anyone have any suggestions?
有没有人有什么建议?
采纳答案by Cross
I dont know if it's too late, but I found the answer after some trial and error, mainly the problem (in my machine) was that I compiled the nodejs on windows to be able to create the extension using visual C++, and I already had installed the nodejs from the page, if I try to run the test using the default installation (which was added to my PATH by the nodejs installer) then it fails, but if I use the compiled node.exe (the one I compiled to be able to reference the libs in Visual C++) then it works.
我不知道是不是太晚了,但经过反复试验,我找到了答案,主要问题(在我的机器上)是我在 windows 上编译了 nodejs 以便能够使用 Visual C++ 创建扩展,而我已经有了从页面安装了 nodejs,如果我尝试使用默认安装(由 nodejs 安装程序添加到我的 PATH 中)运行测试,那么它会失败,但是如果我使用编译的 node.exe(我编译为能够引用 Visual C++ 中的库)然后它就可以工作了。
In summary, the problem is not with the extension, it's with the nodejs compilation, use the node that you compiled (in order to build the VS solution I assume you did that) and then it should work on the remote machine.
总之,问题不在于扩展,而在于 nodejs 编译,使用您编译的节点(为了构建 VS 解决方案,我假设您这样做了),然后它应该可以在远程机器上运行。
Note: The problem relies on that you're using node.exe compiled in 64bits to run a 32bits dll, that's why it's complaining, if you use node.exe in 32 bits it should work. (at least that solved my problem)
注意:问题在于您使用的是 64 位编译的 node.exe 来运行 32 位 dll,这就是它抱怨的原因,如果您使用 32 位的 node.exe 它应该可以工作。(至少解决了我的问题)
回答by Dave
Just had the same problem and even though the architectures of my node and addon were identical, I got similar errors messages. It turns out that you can't rename the node executable. It has to be node.exe, I was trying to test multiple versions at the same time so I had to put them in their own folders. After that it all worked fine.
只是遇到了同样的问题,即使我的节点和插件的架构相同,我也收到了类似的错误消息。事实证明,您无法重命名节点可执行文件。必须是node.exe,我试图同时测试多个版本,所以我不得不将它们放在自己的文件夹中。之后,一切正常。
回答by Stefano Borzì
In my case, the issue was trying to execute an Electron app on Windows that was built (for Windows) using Linux. I solved by building it (for Windows) using Windows.
就我而言,问题是尝试在使用 Linux 构建(适用于 Windows)的 Windows 上执行 Electron 应用程序。我通过使用 Windows 构建它(对于 Windows)来解决。
To build it on windows I used the following commands:
为了在 Windows 上构建它,我使用了以下命令:
npm install --global-production windows-build-tools
npm install
npm run build:prod && electron-builder build --windows
To execute the last command you need electron-builder, install it if you do not have with
要执行最后一个命令,您需要electron-builder,如果没有,请安装它
npm install --save-dev electron-builder
回答by Robert Calhoun
Unrelated to your probem: I get the same error (Error: %1 is not a valid Win32 application) when trying to execute a script with extension ".node", e.g. node.exe example.node. Other extensions (.js, .txt, no extension at all) work fine.
与您的问题无关:Error: %1 is not a valid Win32 application尝试执行扩展名为“.node”的脚本时,我遇到相同的错误 ( ),例如node.exe example.node. 其他扩展名(.js、.txt,根本没有扩展名)工作正常。

