iTwin / imodel-native

Apache License 2.0
20 stars 12 forks source link

Wrap import error of native addon on windows with suggestion to install MSVC runtime #287

Closed MichaelBelousov closed 1 year ago

MichaelBelousov commented 1 year ago

Occasionally on windows desktops people seem to run into an issue where node (or electron) throws an error when loading the native addon, with the windows error message The specified module could not be found.

image https://github.com/iTwin/imodel-native/blob/78720da4c7fd133a891d14a27bb1a013bbdeb564/iModelJsNodeAddon/api_package/ts/src/NativeLibrary.ts#L94

This happens because when node.js/electron requires the native addon, windows fails to load the addon DLL due to a missing dependency of the DLL, frequently this is just the Microsoft Visual C++ Runtime not being installed on the system. I have not identified any other required DLLs dependencies that windows 10/11 shouldn't already have installed.

The require occurs here

Could it be wrapped in a clearer platform-specific warning? like so:

try {
  this._nativeLib = require(`./${NativeLibrary.archName}/${NativeLibrary.nodeAddonName}`) as typeof IModelJsNative;
} catch (err) {
  if (/The specified module could not be found/.test(err.message) && process.platform === 'win32')
    err.message += "\niTwin.js requires that Microsoft Visual C++ Runtime be installed."
      + "\nThe latest version is available at Microsoft's website."
      + "\nhttps://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist";
  throw err;
}
MichaelBelousov commented 1 year ago

If there is no disagreement I will submit a PR

calebmshafer commented 1 year ago

@MichaelBelousov I'd suggest in the message that one possible reason this can happen is a missing Runtime and check to make sure it's installed. There are other reasons the specified module could not be found error could occur.

Are app devs hitting this issue often?

MichaelBelousov commented 1 year ago

@MichaelBelousov I'd suggest in the message that one possible reason this can happen is a missing Runtime and check to make sure it's installed. There are other reasons the specified module could not be found error could occur.

Are app devs hitting this issue often?

Whoops I meant to word it as a possible reason, not the only. In my experience building itwin.js desktop apps it's happened maybe to 5ish people.

aruniverse commented 1 year ago

Maybe reference https://www.itwinjs.org/learning/supportedplatforms/#backend-prerequisites