2

I'm trying to load a custom module in electron written in D with node_dlang package, which is fine with node, but it fails within electron.

the test with node, that runs just fine, goes like this:

const nativeModule = require('./module.node');
const assert = require('assert');

assert(nativeModule != null);
assert(nativeModule.ultimate() == 42);

But when I went to use it within electron.js, through the preload script, it returns in an error.

the preload script goes like this:

const {
    contextBridge,
    ipcRenderer
} = require("electron");

const nativeModule = require('./module.node');
const assert = require('assert');

assert(nativeModule.ultimate() == 42);

function pageLoaded()
{
   // ...
}

window.addEventListener('DOMContentLoaded', pageLoaded);

the error when I attempt to load the module within electron application is:

A JavaScript error occured in the browser process --------------------------- Uncaught Exception: Error: A dynamic link library (DLL) initialization routine failed.

\\?\C:\Users\001\Desktop\ele\module.node
    at process.func [as dlopen] (VM70 asar_bundle.js:5)
    at Object.Module._extensions..node (VM43 loader.js:1138)
    at Object.func [as .node] (VM70 asar_bundle.js:5)
    at Module.load (VM43 loader.js:935)
    at Module._load (VM43 loader.js:776)
    at Function.f._load (VM70 asar_bundle.js:5)
    at Function.o._load (VM75 renderer_init.js:33)
    at Module.require (VM43 loader.js:959)
    at require (VM50 helpers.js:88)
    at Object.<anonymous> (VM88 C:\Users\001\Desktop\ele\preload.js:6)

What's causing this and how do I fix this?

version

node version is: v14.17.0
electron.js: v13.1.1

both are 64-bit.

the module source code goes like this:

import std.stdio : stderr;
import node_dlang;

extern(C):

void atStart(napi_env env)
{
    import std.stdio;
    writeln ("Hello from D!");
}

int ultimate()
{
    return 42;
}

mixin exportToJs! (ultimate, MainFunction!atStart);

it's compiled with dub command line. No arguments.

UPDATE 1 Do I need to rebuild this module? I found this but it didn't work for me either. I installed the electron-rebuild package by npm install --save-dev electron-rebuild and rebuild with .\node_modules\.bin\electron-rebuild.cmd -v 13.1.1 the command ran fine but I still got same error.

UPDATE 2: inside the console, I clicked in the javascript source code file link in the error message (from the exception) it points to this line of code, where there's this comment saying that:

no static exports found

enter image description here

what does that mean? is this related to the methods in D class? they're marked as public... not sure if related

Jack
  • 16,276
  • 55
  • 159
  • 284
  • My guess is that the dll's made by that extension do not support multiple initialization by default. See [this](https://nodejs.org/api/addons.html#addons_context_aware_addons). The best option here would probably be opening an issue on [`node_dlang` github page](https://github.com/NotSpooky/node_dlang). – Anton Curmanschii Jun 12 '21 at 07:40
  • Actually, no, see [this](https://stackoverflow.com/a/55333503/9731532). Probably because of that, I'm not able to load the dll even from the main file. – Anton Curmanschii Jun 12 '21 at 08:23

2 Answers2

0

Electron is a Windows-Application and therefore you need to remove output to std. Try to remove

import std.stdio : stderr;

and

import std.stdio;
writeln ("Hello from D!");

and retry import to Electron.

Mirco Ellmann
  • 983
  • 7
  • 8
0

Please refer this (https://stackoverflow.com/a/74280836/9558119) post from me regarding same. Since it is electron build might be missing Visual C++ Build Environment: Visual Studio Build Tools

Sarang Kakkoth
  • 381
  • 3
  • 9