mottosson
mottosson

Reputation: 3753

Electron: Include binary dependency with Webpack

I'm trying to use Webpack in my electron project to bundle the Typescript code in my main process (the renderer is an Angular project managed with the CLI).

But in my main process I have a dependency on registry-js:

import { enumerateValues, HKEY } from "registry-js";

Registry-js is built to a .node-binary. So my question is how I can include that in my bundle? Currently my webpack.config.js looks like this:

const path = require("path");

module.exports = {
  entry: "./main.ts",
  target: "electron-main",
  output: {
    path: "./dist-main",
    filename: "main.bundle.js",
  },
  mode: "production",
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      }
    ],
  },
  resolve: {
    extensions: [".ts", ".js"],
    alias: {
      "registry-js": path.join(__dirname, "node_modules/registry-js/build/Release/registry.node"),
    }
  }
};

When I start my application I get a TypeError: Cannot read property 'HKEY_LOCAL_MACHINE' of undefined

What can I do to include the binary in my Webpack build to be able to run my application in dev mode and as production build?

Upvotes: 2

Views: 792

Answers (1)

OJ Kwon
OJ Kwon

Reputation: 4641

Webpack cannot bundle native addon modules. You should use appropriate loader like node-loader or node-addon-loader and copy binaries when create package.

Upvotes: 2

Related Questions