Allow electron `requires` to passthrough untouched in Vite

Viewed 1224

When trying to use custom-electron-titlebar , I get an error when it tries to access path . I've added

build: { rollupOptions: { external: ["electron", ...builtinModules],   }}

to my vite config, and that seems to allow me to require('path') in my own code just fine, but when the imported code inside custom-electron-titlebar tries to do the same, it fails to work. It appears to be a wrapped/proxy object, if I call it directly I get an error.

Module "path" has been externalized for browser compatibility and cannot be accessed in client code.

Is there a way to get these requires to passthrough as well?

1 Answers

As mentioned in vitejs/vite#2985, the plugin vite-plugin-commonjs-externals can fix this for us. In vite.config.js:

import commonjsExternals from 'vite-plugin-commonjs-externals';
export default {
  plugins: commonjsExternals({
    externals: ['path'],
  }),
};
Related