Node.js native module is not a valid Win32 application error
Asked Answered
M

5

19

Trying to make Hello World native module for node.js

Got an Win32 Project in VS 2012 with one file:

#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

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:

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)

What I tried:
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

Does anyone have any suggestions?

Machzor answered 23/10, 2012 at 17:10 Comment(0)
P
15

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.

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.

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)

Progeny answered 27/11, 2012 at 20:8 Comment(3)
I changed my node.exe to 32 bit and that worked with 32 bit addonsBlossom
I'm having this same problem, how do you change it, to node.exe to 32 bit, says im running 10.22 ia32Jolyn
you can use nvm for windows: github.com/coreybutler/nvm-windowsConway
G
0

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.

Gracioso answered 19/8, 2015 at 6:23 Comment(0)
C
0

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.

To build it on windows I used the following commands:

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

npm install --save-dev electron-builder
Crutch answered 27/2, 2020 at 22:16 Comment(2)
the third command gave me this: npm ERR! missing script: build:prodUnaware
probably you miss the npm script in package.json, it consists of "build:prod": "npm run build -- -c production", so you could run npm run build -- -c productionMillar
K
0

Using Electron Forge webpack typescript boilerplate. This is what worked for me:

In webpack.main.config.js add 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

Kraul answered 20/4, 2021 at 14:30 Comment(0)
F
-2

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.

Fenwick answered 12/12, 2012 at 19:47 Comment(1)
.node files are wrapped c++ files.Cartel

© 2022 - 2024 — McMap. All rights reserved.