Skip to content

Hello world native extension example doesnt load #1168

@matthewn4444

Description

@matthewn4444

Hello, i have been struggling for hours on this. I have been able to get native node extensions to work on node webkit in the past but I am trying electron and i have zero success.

I am building the extension with rebuild and then loading it in the preload script and i get the generic error when i try to load up the application

Error: Module did not self-register: '\\?\C:\project\build\Release\extension.node'.

My folder structure is as such

- build
- node_modules
- native
       - source.cc
- lib
       - <js files>
- binding.gyp
- index.html
- package.json
- preload.js

binding.gyp

{
  "targets": [
    {
      "target_name": "extension",
      "sources": [ "native/extension.cc" ],
    }
  ]
}

extension.cc

#include <v8.h>
#include <node.h>


static void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(
      v8::String::NewFromUtf8(isolate, "world").ToLocalChecked());
}


static void InitModule(v8::Local<v8::Object> exports,
                       v8::Local<v8::Value> module,
                       v8::Local<v8::Context> context) {
  NODE_SET_METHOD(exports, "hello", Method);
}

preload.js

const binding = require("./build/Release/extension.node")

package.json

{
  "name": "project",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@electron/rebuild": "^3.7.1",
    "electron": "^33.3.1",
    "node-gyp": "^11.0.0"
  },
  "dependencies": {
    "@electron/remote": "^2.1.2",
    "electron-json-storage": "^4.6.0",
    "jquery": "^3.7.1",
    "koffi": "^2.10.0",
    "node-fetch": "^2.6.1"
  }
}

I am not sure what is the problem. The extension builds fine. Used .\node_modules\.bin\electron-rebuild.cmd and said it completed.

The electron verison is 33.3.1 and is x64. I am on Windows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions