128

I try to use node-vlc with nw.js (v0.12.0-alpha2). When i launch my app without nw.js it works, but when i launch it with nw.js i got an error:

Uncaught Error: Module did not self-register.", source: /home/alexis/Bureau/dev/jukebox/node_modules/vlc/node_modules/ffi/node_modules/bindings/bindings.js (84)

I tried some commands with nw-gyp but it couldn't help me. I am on Ubuntu 14, 64-bit.

Jannis Lehmann
  • 1,428
  • 3
  • 17
  • 31
Alexis Schad
  • 1,436
  • 2
  • 10
  • 8

15 Answers15

202

If you've upgraded node then npm rebuild might fix this for you

DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • 3
    You should definitely run this _without_ sudo unless you know you actually need root access. You shouldn't need root unless you're dealing with globally installed modules (which is generally discouraged). – gfxmonk Apr 23 '15 at 07:34
  • 13
    Didn't work for me, but `rm -r node_modules` then `npm install` did the job – Spc_555 Nov 25 '15 at 20:14
  • 1
    Not only limited to upgraded, also work in the case of reinstalling npm and node, etc. – Kevin Dec 06 '15 at 21:48
  • This doesn't really answer the question. What is the problem? I get this error loading module 'usb' from a web worker in electronJS. No idea what it means, can you explain what the problem is? – PeterT Jun 02 '20 at 23:00
  • It didn't work. In fact non of these answers here works for me. I am using drivelist in a Webpack Electron project. – Tung Aug 12 '21 at 02:21
  • 1
    Tung and @PeterT in cases where the module may be loaded more than once in the same process (e.g. Electron, worker_threads) modules with native add-ons need to be "context aware" (see https://github.com/nodejs/node/blob/main/doc/api/addons.md#context-aware-addons). It seems likely that the modules you are using are not context aware, resulting in this error being thrown. You cannot address this as a user of the module, the module itself needs to be fixed. In most cases this seems to a pretty small change: https://github.com/nodejs/node/issues/21783#issuecomment-429637117 – Stevie Mar 03 '23 at 11:52
67

For me: rm -r node_modules then npm install

thorn0
  • 9,362
  • 3
  • 68
  • 96
Thami Bouchnafa
  • 1,987
  • 1
  • 15
  • 21
24

I had a similar issue with another product and my fix was to change the version of node I was using. I was using 0.12.0 and changed back to 0.10.26.

Personally, I use NVM to handle node version changing. With NVM installed it's as simple as running

nvm use 0.10.26

Or setting the default version to 0.10.26

nvm alias default 0.10.26

Hopefully this helps you out - our issues came from different products but the solution may be the same.

joel.software
  • 1,505
  • 1
  • 12
  • 15
  • This fix helped me when I was executing global packages but wanted to run local packages with a new node version. Just has to switch versions when I ran global commands. – Collin Price Oct 01 '15 at 17:48
13

I had similar problem.

/Users/user/NodeAddons/bridge/node_modules/bindings/bindings.js:83 Error: Module did not self-register.

In my case I was doing a C/C++ Add-on, and I had forgotten to export the add-on, in my main.cc was missing the code below:

void Init(v8::Handle<v8::Object> exports) {
  NODE_SET_METHOD(exports, "method", method);
}

NODE_MODULE(method, Init);

Hope this helps others! Thanks :)

Aion
  • 131
  • 1
  • 4
  • Glad for you (Y) :D – Aion May 25 '17 at 01:19
  • Aion therefore adding NODE_MODULE macro to the end of file, identical to yours, the module cannot be loaded after compiling it. What could I'm doing wrong? I wanted to open a new question therefore your asnwer is almost identical to the nodejs docs. Minus the semicolon after NODE_MODULE. – lukaswilkeer Oct 03 '21 at 01:52
4

I once had this problem when creating a multi-file c++ addon. In my binding.gyp file I had:

"sources": ["src/*.cc", "src/*.h" ]

And my project contained several *.cc files. However, the NODE_MODULE() macro was called only on one file which imported the rest of the files. But node expects that it is called on the frist *.cc file listed in sources. So I had to change sources to explicitly add that file to the beginning

user1485083
  • 161
  • 1
  • 5
3

I've add the same issue because I installed to modules as sudo... Removing the node modules folder and reinstalling as normal user fixed it.

silveur
  • 157
  • 1
  • 9
  • 1
    Perfect. I changed the ownership of the folder using chown and re-installed with npm without sudo. It worked. – Asheesh Feb 21 '17 at 12:49
3

For me npm rebuild or npm update didn't work. I had to remove the node_modules folder and run npm install to install them again.

Jesús Carrera
  • 11,275
  • 4
  • 63
  • 55
2

For me, running npm update worked

inolasco
  • 685
  • 8
  • 14
  • It can, but only if the package is outdated. It also does something else than asked for. `npm rebuild` or `rm -rf node_modules && npm i` is the correct answer. – Albin Sep 06 '16 at 14:45
2

I was getting an internal error: Module did not self-register.

  1. Deleted the node_modules folder
  2. ran npm install

It worked just fine.

Nandhini
  • 645
  • 7
  • 21
2

I had this error with Snappy. Was using Node 11. Checked Snappy's NPM page https://www.npmjs.com/package/snappy where they listed which versions of node they supported.

Deleting node_modules folder rm -rf node_modules and then reinstalling using the correct version of Node resolved it.

One of the versions they supported on Linux at the time of this writing was Node version 12.

nvm deactivate 11
nvm uninstall 11
nvm install 12
nvm use 12

Problem solved


Another cause of this problem: if you're using pm2, then after upgrading node you may need to reinstall pm2. Test whether pm2 is the issue by running your app without pm2 node server.js then with pm2: pm2 start server.js.

Proper way to update PM2 after updating Node.js

David
  • 895
  • 8
  • 12
2

Rebuild your C++ add-ons.

Did you encounter something like this?

Module did not self-register: '…\node_modules\@u4\opencv4nodejs\build\Release\opencv4nodejs.node

It’s likely that you have just updated your Node.js. Once you updated your Node.js, you need to rebuild your C++ add-ons, Node.js packages written in C++.

Why

When you build Node.js’ C++ add-ons, they get compiled into require-able .node files and linked to the currently installed Node.js ABI library, which is not compatible with other versions of it. Your packages were built only compatible with the specific version of Node.js.

How

Firstly, try npm rebuild. If your C++-add-on-based packages have a build script in their package.jsons, it’ll do. If it doesn’t, you need to manually build your C++ native add-on packages. Do again what you did when you were installing such packages. Refer to the building instructions in the packages’ documentations to rebuild them. Or try reinstalling (npm install) them.

1

I had this same issue with 0.12 and io.js 1.3.0, reverting to Node.js 0.10 fixed the issue.

Matthew O'Riordan
  • 7,981
  • 4
  • 45
  • 59
0

I had the same problem. My script that was referencing a global reference script had an invalid reference. I took off that invalid reference and the error was gone. My error message had no indication of that particular invalid reference which made it harder to debug. But 'Uncaught Error: Module did not self-register' was the message I was getting.

This also happen in my other project. For some reason, it wouldn't recognize the reference path if one of the characters are uppercase. Even thought, the upper-casing was the correct spelling of the path.

Good4Nothing
  • 49
  • 1
  • 4
0

I had this issue while setting up my Cypress project.

I found out the issue was caused because Cypress uses node from its bundle version by default (which was version 8.0 in my case) , whilst the package I wanted to use required the node version to be 10 or higher.

I did have node 12.0 installed on my machine but since cypress was not using that I had to add the line shown below in the settings file (cypress.json) to set the value for 'nodeVersion' to 'system', to tell cypress explicitly to use the node version installed on my machine.

Add this line to your settings file:

**"nodeVersion": "system"**
Abdelaziz
  • 1
  • 1
0

Not mentioned in other answers:

Many npm modules with native add-ons are not context aware, which is an issue if you are working in an environment where the module may be loaded more than once in different contexts (e.g. worker_threads, electron, or any other env with isolated contexts that have their own require cache).

In these cases it isn't something you can fix in your project, it has to be addressed in the module itself.

For many modules it seems the fix is pretty small: https://github.com/nodejs/node/issues/21783#issuecomment-429637117

Though for others it can be more involved. More info here: https://github.com/nodejs/node/blob/main/doc/api/addons.md#context-aware-addons

Stevie
  • 7,957
  • 3
  • 30
  • 29