node-loader is a Webpack loader designed to facilitate the bundling and usage of Node.js native add-ons (files with the `.node` extension) within Webpack-powered applications. It is particularly useful for projects targeting Node.js environments, including Electron's main and renderer processes. The current stable version is `2.1.0`, released in November 2024. The project generally follows an irregular release cadence, focusing on compatibility with newer Webpack and Node.js versions, as well as addressing specific issues like platform compatibility (e.g., macOS dlopen). Its primary differentiator is its specific focus on handling native Node.js modules, a niche that requires careful configuration within Webpack due to the binaries involved. It handles copying the native module to the output directory and ensures it can be dynamically loaded at runtime.
npm install node-loaderVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure webpack with `node-loader` to process native `.node` modules. It includes the essential `target: 'node'` and `node: { __dirname: false }` settings, and an example of importing a native module in application code.
Upgrade your project's webpack version to `^5.0.0` or higher, or explicitly install `node-loader@^1.0.0`.
Ensure your Node.js runtime is `10.13.0` or newer and your Webpack version is `4.0.0` or newer. If not, use `node-loader@^0.6.0`.
Set `target: 'node'` (or an appropriate Electron target) in your `webpack.config.js` when bundling code that uses native Node.js modules.
Add `node: { __dirname: false, __filename: false }` to your `webpack.config.js` configuration.Ensure you have a `module.rules` entry for `test: /\.node$/` with `loader: 'node-loader'` in your `webpack.config.js`. Also, verify `target: 'node'` and `node: { __dirname: false }` are set.Change `target` in `webpack.config.js` to `node`, `async-node`, `electron-main`, `electron-renderer`, or `electron-preload`.
Add `node: { __dirname: false, __filename: false }` to your `webpack.config.js` to prevent Webpack from polyfilling these globals.Ensure the `flags` option in `node-loader` configuration is a number, typically from `os.constants.dlopen` (e.g., `os.constants.dlopen.RTLD_NOW`). Remember to `require('os')` if using these constants.