简体   繁体   English

Node.js 本机模块不是有效的 Win32 应用程序错误

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

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.这将编译为 hello.node。
Options:选项:
- Dynamic Library (.dll) - 动态库 (.dll)
- No Common Language Runtime Support - 没有公共语言运行时支持

Use it like:像这样使用它:

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

It works on local machine (win8 x64, node: 0.8.12)它适用于本地机器(win8 x64,节点: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:但是在远程服务器(windows server 2008 x64,节点: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. %1 不是有效的 Win32 应用程序。

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

What I tryed:我尝试了什么:
Playing with app pool settings (enable win32 apps) does not helped.使用应用程序池设置(启用 win32 应用程序)没有帮助。
Iisnode x86 does not install on x64 os. iisnode x86 不能安装在 x64 操作系统上。
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由于错误无法编译为 x64:错误 2 错误 LNK1112:模块机器类型 'X86' 与目标机器类型 'x64' 冲突 C:\\derby\\hello\\build\\node.lib(node.exe) hello

Does anyone have any suggestions?有没有人有什么建议?

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.注意:问题在于您使用的是 64 位编译的 node.exe 来运行 32 位 dll,这就是它抱怨的原因,如果您使用 32 位的 node.exe 它应该可以工作。 (at least that solved my problem) (至少解决了我的问题)

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.它必须是node.exe ,我试图同时测试多个版本,所以我不得不将它们放在自己的文件夹中。 After that it all worked fine.之后,一切正常。

In my case, the issue was trying to execute an Electron app on Windows that was built (for Windows) using Linux.就我而言,问题是尝试在使用 Linux 构建(适用于 Windows)的 Windows 上执行 Electron 应用程序。 I solved by building it (for Windows) using Windows.我通过使用 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

Using Electron Forge webpack typescript boilerplate.使用 Electron Forge webpack typescript 样板。 This is what worked for me:这对我有用:

In webpack.main.config.js add externals: ['sqlite3'] :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']
};

Source 来源

Alternatively -that also worked for me-, you can use better-sqlite3 , as suggested here或者 - 这也对我有用-,您可以使用better-sqlite3 ,如建议here

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", eg node.exe example.node .与您的问题无关:尝试执行扩展名为“.node”的脚本(例如node.exe example.node )时,我遇到相同的错误( Error: %1 is not a valid Win32 application )。 Other extensions (.js, .txt, no extension at all) work fine.其他扩展名(.js、.txt,根本没有扩展名)工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js 错误:%1 不是有效的 Win32 应用程序 - Node.js error : %1 is not a valid Win32 application 错误:在C ++中制作node.js库时,%1不是有效的Win32应用程序 - error: %1 is not a valid Win32 application while making node.js library in c++ (Node.js) --grpc_out: protoc-gen-grpc: %1 不是有效的 Win32 应用程序 - (Node.js) --grpc_out: protoc-gen-grpc: %1 is not a valid Win32 application 错误消息:Node.exe不是有效的Win32应用程序 - Error Message: Node.exe is not a valid Win32 Application 错误:Nodejs keytar.node 不是有效的 Win32 应用程序 - Error: Nodejs keytar.node is not a valid Win32 application 错误:bcrypt_lib.node 不是有效的 Win32 应用程序 - Error: bcrypt_lib.node is not a valid Win32 application node printer.node 不是有效的 win32 应用程序 - node printer.node is not a valid win32 application 电子抛出错误 %1 不是带有自定义节点插件的有效 win32 应用程序 - Electron throwing error %1 is not a valid win32 application with custom node addon npm start给出错误消息server.js不是有效的win32应用程序 - npm start gives the error that server.js is not a valid win32 application Gulp-Sass错误:%1不是有效的Win32应用程序 - Gulp-Sass error: %1 is not a valid Win32 application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM