microsoft / node-api-dotnet

Advanced interoperability between .NET and JavaScript in the same process.
MIT License
426 stars 49 forks source link

Unable to Compile with Webpack #300

Closed sbowler closed 2 weeks ago

sbowler commented 1 month ago

I have a NodeJs webserver that currently utilizes Webpack 5.75.0. Due to the way this library dynamically requires/imports the needed dll file, I receive a warning from Webpack Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ./node_modules/node-api-dotnet/init.js @ ./node_modules/node-api-dotnet/index.js 1:19-36

This ends up with a runtime error when I try to require node-api-dotnet An unknown error occurred: Error: Cannot find module '....WebServer/win-x64/Microsoft.JavaScript.NodeApi.node'

From what I've read it sounds like it's generally bad practice to dynamically require files since it can cause issues like this, though I'm not sure what a better approach would be. I've been trying to figure out a way to work around the issue and didn't see anything in your documentation or tracked issues about this. Any help would be appreciated.

jasongin commented 1 month ago

There is some discussion of a similar issue at https://github.com/microsoft/node-api-dotnet/issues/264#issuecomment-2042115327

I think the code could be changed to use non-dynamic requires, it would just be more verbose.

sbowler commented 1 month ago

I thought that as well, but it is a bit different or potentially a different solution dealing with Electron.

I think perhaps a switch statement to include the correct library based on the different variables might work better, but as you said would be much more verbose.

jasongin commented 2 weeks ago

Fixed by https://github.com/microsoft/node-api-dotnet/pull/303