Electron + Vite. Issues with using Node modules


When using Vite to build Electron (which currently does not support the "type": "module" option in package.json), there may be a problem using Node.js modules such as path, fs, and others.

An error Error: Module “path” has been externalized for browser compatibility. Cannot access “path.sep” in client code will appear.

However, this can be easily fixed by installing the vite-plugin-electron-renderer polyfill (npm i -D vite-plugin-electron-renderer) and declaring it in the vite.config.ts like this:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import renderer from 'vite-plugin-electron-renderer';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    renderer(),
  ],
  base: './',
});

TAGS:

  • Electron
  • Vite
  • Node.js
  • TypeScript
  • Frontend
  • Development
  • WebDevelopment
  • BuildTools
  • Troubleshooting