1

I made a simple c++ n-api module which works when started with node but fails to load in reactjs / webpack

const addon = require('./build/Release/module');

Module not found: Error: Can't resolve

The path to module is correct.

I'm using version: "react": "^16.8.6", "webpack": "^4.42.1", "node-addon-api": "^3.0.0"

Adding some info because the commenter didn't read it correctly which is probably my fault.

node-gyp compiles and create the binary "module.node"

Node reads this file when used in require function (within js) but reactjs doesn't understand it. example of the js file which works with nodejs:

const addon = require('./build/Release/winhandler');
var handle = addon.getWindowHandle();
console.log(`window handle ${handle}`);
addon.focusWindow(handle);
addon.minimizeWindow(handle);

so again, NodeJs reads it and functions can be called and work. ReactJs fails at "require"

what is needed (missing something in compile options or need different tools) to make this work with reactjs.

Hope someone can help me. Thank you.

Reneb
  • 121
  • 2
  • 9
  • make sure you are exporting something from './build/Release/module' – Yilmaz Aug 01 '20 at 07:33
  • I have the same problem. @Yilmaz, why would this be relevant ? On my end, the module.node addon works fine when testing it from the repo in which I build it. But it does not work when I publish this addon as an npm package, and import from node_modules... The difference is that the importing repo is a ReactTS project – Tristan Duquesne Dec 17 '20 at 00:36
  • Here's how I solved the issue on my end, if it helps anyone: https://stackoverflow.com/questions/65375807/importing-requiring-an-napi-addon-from-a-webpack-reactjs-reactts-project – Tristan Duquesne Dec 30 '20 at 18:24

0 Answers0