Vite plugin (v2.2.1) for seamless integration of Node.js native modules (.node files) into Vite builds. Supports Electron, node-gyp-build, and bindings package patterns. Offers automatic file hashing, cache busting, and zero-config setup for most use cases. Compatible with Vite 3–7. Key differentiator: resolves native module imports in Vite/Rollup, which unlike Webpack, lack built-in .node file handling. Includes support for NAPI-RS and custom native file extensions since v2.2.0.
npm install vite-plugin-native-modulesVerified import paths — ran on the pinned version, not inferred.
Basic setup: add plugin to vite config, import a .node file, and use it.
Set `forced: true` in plugin options when running in dev mode.
Stick with default 'preserve' format unless you have a specific need.
Use `import nativeFilePlugin from 'vite-plugin-native-modules'` instead of `require('vite-plugin-native-modules')`.Ensure your code imports the .node file directly, or configure `additionalNativeFiles` for packages with non-standard extensions.
Add `build.rollupOptions.external: ['electron']` in vite config.
Install the package: `npm install vite-plugin-native-modules`. Ensure tsconfig.json has `moduleResolution: 'bundler'` or `node16`.
Use `nativeFilePlugin()` without `new`.
Add the plugin to vite config: `import nativeFilePlugin from 'vite-plugin-native-modules'` and add `plugins: [nativeFilePlugin()]`.
Install a compatible Vite version: `npm install vite@^5.0.0` or upgrade/downgrade accordingly.