Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello world native extension example doesnt load #1168

Closed
matthewn4444 opened this issue Jan 20, 2025 · 1 comment
Closed

Hello world native extension example doesnt load #1168

matthewn4444 opened this issue Jan 20, 2025 · 1 comment

Comments

@matthewn4444
Copy link

matthewn4444 commented Jan 20, 2025

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.

@matthewn4444 matthewn4444 changed the title Hello world example doesnt load Hello world native extension example doesnt load Jan 20, 2025
@matthewn4444
Copy link
Author

I figured it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant