Skip to content

requiring C++ addon modules occurs 'Error: Module did not self-register.' with Worker Threads #21481

Description

@ledsun
  • Version: v10.5.0
  • Platform: Darwin nakajimashigeru-no-MacBook-Pro.local 16.7.0 Darwin Kernel Version 16.7.0: Fri Apr 27 17:59:46 PDT 2018; root:xnu-3789.73.13~1/RELEASE_X86_64 x86_64
  • Subsystem: worker

When C++ addon was loaded twice with worker threads, error below occurs:

events.js:167
      throw er; // Unhandled 'error' event
      ^
Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:731:18)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at bindings (/Users/shigerunakajima/worker_thread_test/node_modules/bindings/bindings.js:81:44)
    at Object.<anonymous> (/Users/shigerunakajima/worker_thread_test/node_modules/libxmljs/lib/bindings.js:1:99)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
Emitted 'error' event at:
    at Worker.[kOnErrorMessage] (internal/worker.js:281:10)
    at Worker.[kOnMessage] (internal/worker.js:291:37)
    at MessagePort.Worker.(anonymous function).on (internal/worker.js:228:57)
    at MessagePort.emit (events.js:182:13)
    at MessagePort.onmessage (internal/worker.js:57:8)

To reproduce, run code below with --experimental-worker flag:

const libxmljs = require('libxmljs')
const {
  Worker
} = require('worker_threads')

new Worker(__filename)

When I try to require the sharp, same error occurs.

Activity

  1. added
    workerIssues and PRs related to the worker_threads module and Worker API.
    on Jun 23, 2018
  2. added
    addonsIssues and PRs related to native addons.
    on Jun 25, 2018
  3. hlolli commented on Aug 29, 2018

    @hlolli

    I get the same with node-speaker, I'm on Linux nixos 4.14.63 #1-NixOS SMP Wed Aug 15 16:13:02 UTC 2018 x86_64 GNU/Linux with nodejs 10.7.0. The package node-speaker doesn't work with experimental workers, but works fine otherwise. Like @ledsun I'm also self referencing with __file when constructing a new worker.

  4. mattolson commented on Oct 10, 2018

    @mattolson

    I'm running into this as well when using experimental workers that try to load the lzo package.

  5. mattolson commented on Oct 24, 2018

    @mattolson

    This looks to be a duplicate of #21783. It appears that the issue is due to addon initialization -- any native addons that are loaded in both the parent and worker thread (i.e. loaded more than once in the same process) need to be context aware.

    I was facing this issue with the node-lzo library and we fixed it in schroffl/node-lzo#11

  6. bdotsamir commented on Feb 6, 2020

    @bdotsamir

    Issue occurs here too, Linux host-name 5.3.0-28-generic #30~18.04.1-Ubuntu SMP Fri Jan 17 06:14:09 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    Package: brain.js
    Node.js v10.18.1

  7. added a commit that references this issue on Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addonsIssues and PRs related to native addons.workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions