Skip to content

Instantly share code, notes, and snippets.

@erdesigns-eu
Created December 17, 2024 13:43
Show Gist options
  • Save erdesigns-eu/6b47d6b3d1077efb2c0c080fc94f8e80 to your computer and use it in GitHub Desktop.
Save erdesigns-eu/6b47d6b3d1077efb2c0c080fc94f8e80 to your computer and use it in GitHub Desktop.
Runtime require function to fix loading native .node modules when using PKG
/**
* This function is used to load the native .node module. Since webpack replaces the native require function,
* we need to get the original require function from the global scope.
*
* Found this code here: https://github.com/prebuild/node-gyp-build/blob/master/node-gyp-build.js
*/
// @ts-ignore
const runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
const nativeModule = runtimeRequire('./path/to/module.node');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment