How to Use Native Node.js Modules with Electron: Complete Guide to Rebuilding and Loading

Native Node.js modules must be rebuilt against Electron’s headers before they can load because Electron uses a different ABI than standard Node.js, featuring Chromium’s BoringSSL and a custom V8 build.

Working with native addons in Electron applications requires understanding how the runtime handles binary modules. Since Electron ships with its own embedded Node.js runtime that differs from the standard Node.js distribution, any compiled .node files must be specifically targeted for Electron’s environment. According to the electron/electron source code, the framework intercepts process.dlopen calls to ensure native modules link against electron.exe rather than node.dll at startup.

Why Native Modules Require Rebuilding for Electron

Electron bundles a customized Node.js runtime compiled directly into the Electron binary. This creates an Application Binary Interface (ABI) incompatibility with native modules built for standard Node.js installations. The runtime uses Chromium’s BoringSSL and a specialized V8 build, which alters the internal structure that native addons expect.

In lib/node/asar-fs-wrapper.ts, Electron overrides the native process.dlopen function and the .node extension loader during startup. This interception rewrites module loading so that addons dynamically link against electron.exe instead of the traditional node.dll reference. Without rebuilding against Electron’s specific headers, attempting to load a native module triggers a version mismatch error because the NODE_MODULE_VERSION constants differ between runtimes.

Three Methods to Rebuild Native Modules for Electron

The @electron/rebuild package automates the entire rebuild process by detecting your current Electron version, downloading the matching headers, and executing node-gyp with the correct parameters. This tool integrates seamlessly with Electron Forge and other packaging workflows.


# Install the rebuild tool as a dev dependency

npm install --save-dev @electron/rebuild

# Rebuild all native dependencies for the current Electron version

./node_modules/.bin/electron-rebuild

To automate this process during development, add a postinstall script to your package.json:

{
  "scripts": {
    "postinstall": "electron-rebuild"
  }
}

Environment Variable Configuration

You can trigger automatic rebuilding during npm install by setting specific npm configuration variables. This method works well for CI/CD pipelines or one-off installations where adding @electron/rebuild as a dependency is undesirable.

export npm_config_target=14.2.0          # Your Electron version

export npm_config_arch=x64
export npm_config_target_arch=x64
export npm_config_disturl=https://electronjs.org/headers
export npm_config_runtime=electron
export npm_config_build_from_source=true

npm install   # Native modules rebuild automatically

Manual node-gyp Invocation

For developers creating native modules from scratch or requiring fine-grained control over the compilation process, direct node-gyp invocation provides maximum flexibility. Specify the target Electron version, architecture, and header distribution URL explicitly.

HOME=~/.electron-gyp node-gyp rebuild \
  --target=14.2.0 \
  --arch=x64 \
  --dist-url=https://electronjs.org/headers

Windows-Specific Considerations: Delay-Load Hook

Starting with Electron 4, Windows builds no longer provide node.dll. Instead, Electron uses a delay-load hook that redirects references from node.dll to the running electron.exe process. When using @electron/rebuild or environment-variable methods, this hook enables automatically. However, manual compilations require explicit configuration in your binding.gyp file.

As documented in docs/tutorial/using-native-node-modules.md, ensure your module configuration includes:

{
  "targets": [
    {
      "target_name": "myaddon",
      "sources": [ "src/myaddon.cc" ],
      "variables": {
        "win_delay_load_hook": "true"
      }
    }
  ]
}

The win_delay_load_hook variable ensures the generated binary can resolve symbols against the Electron executable rather than searching for the non-existent node.dll.

Step-by-Step Implementation Guide

Follow this workflow to integrate native modules into your Electron application:

  1. Identify your target Electron version by checking your package.json or running npm list electron.

  2. Install @electron/rebuild as a development dependency to handle automated rebuilding.

  3. Execute the rebuild command after installing native packages:

    ./node_modules/.bin/electron-rebuild -f -w your-native-module
  4. Verify loading by requiring the module in your main process and checking for the "different NODE_MODULE_VERSION" error. If the module loads without throwing, the ABI matches correctly.

  5. For Windows development, confirm that any custom native modules include win_delay_load_hook: "true" in their binding.gyp configuration.

Summary

  • Electron uses a custom Node.js runtime with a different ABI than standard Node.js, requiring native modules to be rebuilt against Electron-specific headers.
  • The runtime overrides process.dlopen in lib/node/asar-fs-wrapper.ts to route native module loading through electron.exe instead of node.dll.
  • @electron/rebuild provides the most reliable automated solution for rebuilding native dependencies.
  • Windows applications require the win_delay_load_hook configuration in binding.gyp to handle the absence of node.dll in Electron 4 and later.
  • Environment variables offer a lightweight alternative for CI/CD environments when adding rebuild tools is not feasible.

Frequently Asked Questions

Why do I get a NODE_MODULE_VERSION error when loading native modules?

This error occurs when a native module was compiled for a different version of Node.js than the one embedded in Electron. Since Electron ships with a customized Node.js runtime using Chromium’s V8 and BoringSSL, the ABI version numbers differ from standard Node.js installations. You must rebuild the module against Electron’s headers using @electron/rebuild or manual node-gyp with the correct --target flag to resolve the version mismatch.

Can I use prebuilt native modules with Electron?

Prebuilt binaries typically target standard Node.js runtimes and will not work with Electron without rebuilding. The electron-rebuild tool or manual compilation against https://electronjs.org/headers is necessary to generate compatible .node files. Some community packages may provide Electron-specific prebuilds, but you should verify they match your exact Electron version and architecture.

How do I automate native module rebuilding in CI/CD pipelines?

Set the npm configuration environment variables before running npm install to trigger automatic rebuilding during the installation phase. Configure npm_config_target to match your Electron version, npm_config_runtime to electron, and npm_config_disturl to https://electronjs.org/headers. Alternatively, install @electron/rebuild as a dev dependency and add an postinstall script to your package.json to ensure consistent rebuilding across environments.

What is the win_delay_load_hook and why is it needed on Windows?

The win_delay_load_hook is a linker configuration that redirects dynamic library calls from the missing node.dll to electron.exe. Starting with Electron 4, Windows builds no longer ship with node.dll, so native modules must use this hook to resolve their dependencies against the Electron binary itself. When using @electron/rebuild, this happens automatically, but manual builds require setting "win_delay_load_hook": "true" in the binding.gyp file as specified in the official documentation at docs/tutorial/using-native-node-modules.md.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →