Nw.js: ā€œModule did not self-registerā€ when page is reloaded

Created on 2 Dec 2016  Ā·  15Comments  Ā·  Source: nwjs/nw.js

The first time I run my NW.js application the modules are loaded without errors (and works). If I refresh the page (CTRL+R, F5 or SHIFT+F5) I get the error: Module did not self-register. This only happens when ffi or ref is required.

const ffi = require('ffi');
const ref = require('ref');

In Electron it works without problems.

ffi and ref were installed using npm install ffi and npm install ref followed by nw-gyp rebuild --target=0.18.8 --arch=ia32 from each of the two folders node_modules/ffi and node_modules/ref

Any ideas?

nw.js v0.18.8   
Node v7.2.0   
Chromium 54.0.2840.99   
[email protected]  
[email protected]

Here's the full error output:

Uncaught Error: Module did not self-register.(…)
  Module._extensions..node  @   VM1704 module.js:640
  Module.load   @   VM1704 module.js:512
  tryModuleLoad @   VM1704 module.js:471
  Module._load  @   VM1704 module.js:463
  Module.require    @   VM1704 module.js:522
  require   @   VM1705 module.js:20
  bindings  @   C:\Users\Even\Desktop\nwjs-sdk-v0.18.8-win-ia32\app\node_modules\bindings\bindings.js:76
  (anonymous function)  @   C:\Users\Even\Desktop\nwjs-sdk-v0.18.8-win-ia32\app\node_modules\ref\lib\ref.js:5
  Module._compile   @   VM1704 module.js:595
  Module._extensions..js    @   VM1704 module.js:610
  Module.load   @   VM1704 module.js:512
  tryModuleLoad @   VM1704 module.js:471
  Module._load  @   VM1704 module.js:463
  Module.require    @   VM1704 module.js:522
  require   @   VM1705 module.js:20
  (anonymous function)  @   C:\Users\Even\Desktop\nwjs-sdk-v0.18.8-win-ia32\app\node_modules\ffi\lib\ffi.js:6
  Module._compile   @   VM1704 module.js:595
  Module._extensions..js    @   VM1704 module.js:610
  Module.load   @   VM1704 module.js:512
  tryModuleLoad @   VM1704 module.js:471
  Module._load  @   VM1704 module.js:463
  Module.require    @   VM1704 module.js:522
  require   @   VM1705 module.js:20
  (anonymous function)  @   C:\Users\Even\Desktop\nwjs-sdk-v0.18.8-win-ia32\app\FsuipcMonitor.js:22
  Module._compile   @   VM1704 module.js:595
  Module._extensions..js    @   VM1704 module.js:610
  Module.load   @   VM1704 module.js:512
  tryModuleLoad @   VM1704 module.js:471
  Module._load  @   VM1704 module.js:463
  Module.require    @   VM1704 module.js:522
  require   @   VM1705 module.js:20
  self.require  @   VM1758:11
  (anonymous function)  @   mainpage_renderer.js:15
P2 bug triaged

All 15 comments

It should be a bug. Will look to fix it soon.

I can't reproduce this with a simple case and requiring 'ref'. Could you please provide a case?

Sorry, I forgot to mention that I'm using the 32 bit version of NW.JS all modules are installed using --arch=ia32.

Here's a simple case:
main_page.html:

<!DOCTYPE html>
<html>
    <head>
        <h1>Hello world</h1>
    </head>
    <script>
        const ref = require('ref')
    </script>
</html>

Here's my package.json:

{
    "app_name": "app",
    "dependencies": {
        "ref": "^1.3.3"
    },
    "main": "main_page.html",
    "name": "nw-demo",
    "nodejs": true,
    "version": "0.0.1",
    "window": {
        "frame": false,
        "id": "app",
        "title": "app"
    }
}

EDIT:
Hmm, this case actually seems to work now and then.. I will do some more troubleshooting in my big app..

Looks like the error is related to the use of

"chromium-args": "--mixed-context"

in package.json. Placing this line in the file shown in my previous post triggers the error when a page is reloded.

I can't reproduce this issue with the reporter's test case on Linux and nwjs-sdk-0.21.0. It works fine even if I put "chromium-args": "--mixed-context" in package.json.

close until more information is provided.

@rogerwang I have this issue with two windows and mixed-context enabled.
NW version: 0.21.2 , ia32
OS: Win 7

Let's say the window we put in package.json is a loading window (some splash screen). From this loading page we open our main window where we require the 'ffi' module.
The first time you run the app everything will work, but after refreshing the page the error: "Module did not self-register" appears.

It happens only with mixed-context enabled.

Here is the case:
package.json

{
    "name": "test-ffi",
    "main": "loading.html",
    "version": "0.1.0",
    "window": {
        "title": "tets FFI",
        "width": 500,
        "height": 300,
        "frame": false,
        "resizable": false
    },
    "chromium-args": "--mixed-context",
    "dependencies": {
        "ffi": "2.2.0"
    }
}

loading.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Loading page</title>
</head>
<body onload="loaded()">
    <h1>Loading...</h1>
    <script>
        var loadingWin = nw.Window.get();

        function showMainWindow() {
            nw.Window.open('index.html', {
                focus: true,
                position: 'center',
                width: 1000,
                height: 700,
                min_width: 1280,
                min_height: 740,
                frame: true,
                show: false
            }, function (w) {
                w.on('loaded', function () {
                    w.show();
                    loadingWin.close();
                });
            });
        }

        function loaded() {
            setTimeout(showMainWindow, 2000);
        }
    </script>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>FFI test</title>
</head>
<body>
    <h1>Ffi TEst</h1>
    <script>
        var ffi = require('ffi');
    </script>
</body>
</html>

@rogerwang Is there any updates on this issue?

CC @Christywl , please try with @KiresMA 's example.

@KiresMA , I try your sample, but there is an error in my device(Windows 10 64bit).
My steps:

  1. create a folder named 5522 and create package.json loading.html index.html with your codes
  2. npm install
  3. nwjs-sdk-v0.22.1-win-x64\nw.exe 5522\

The app starts, but an error occurs in the devtools console:
5522

I also try to reload the app, there is no "Module did not self-register". May be it's blocked by the above error.
(you said refresh the page, does it mean 'reload the app'? )

Do you have any ideas about this error?

@Christywl "you said refresh the page, does it mean 'reload the app'?" - Yes, it does.

I forgot to mention that after $npm install I'm rebuilding ffi and ref modules with:
$nw-gyp rebuild --target=0.21.2
Maybe this will be the case..

Also please try to use both NodeJS and NWjs ia32 architecture (like I did) or at least just check whether they are both x64.

Here are my device params:

  • Windows 7 - x64;
  • NodeJS - x86, v7.5.0
  • NWjs - ia32, v0.21.2

@KiresMA , thanks for your advice. It works well for me after rebuild ffi and ref. There is no error in the console after I right-click to reload the app.
5522-1

Environments:

- Node: v7.8.0 x64
- NWjs: win-sdk-v0.21.2-x64
- Windows 10 x64
- Node: v7.5.0 x86
- NWjs: win-sdk-v0.21.2-ia32
- Windows 10 x64

@Christywl Try to:

  1. run the app
  2. open dev tools
  3. click F5 OR CTRL+SHIFT+R on the opened DevTools

I reproduce this issue now. Press F5, not right-click to reload the app.

It seems as if you can't re-require native modules (.node files) in Node.js. Can it be related to this?

https://nodejs.org/dist/latest-v8.x/docs/api/modules.html#modules_require_cache
https://github.com/nodejs/node/issues/6160
https://github.com/nodejs/node/issues/5016

I have the same problem, and worked around it for now by initializing the native module only in Node context, and then exposing it to the browser context via global. Only the browser context get reloaded on navigator.reload(). This solution only works without --mixed-context though, as far as I can see.

Was this page helpful?
0 / 5 - 0 ratings