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

Does anyone have any suggestions?

Vladimir Makhaev
  • 1,104
  • 2
  • 10
  • 23

5 Answers5

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)

Cross
  • 1,436
  • 12
  • 20
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.

Dave
  • 7,283
  • 12
  • 55
  • 101
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
Stefano Borzì
  • 1,019
  • 1
  • 15
  • 32
  • the third command gave me this: npm ERR! missing script: build:prod – mfadel Aug 06 '20 at 13:44
  • 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 production` – Stefano Borzì Aug 06 '20 at 14:12
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

Ambroise Rabier
  • 3,636
  • 3
  • 27
  • 38
-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.

Robert Calhoun
  • 4,823
  • 1
  • 38
  • 34