Understand Preload Script Compatibility Matrix
mainWhen using Electron, the compatibility of import vs require in preload scripts depends on your webPreferences configuration. In most cases, using cjs (CommonJS) format for preload scripts is recommended to ensure maximum compatibility across different sandbox and node integration settings.
Refer to the following matrix to determine if import or require will work based on your webPreferences:
| webPreferences | import | require |
|---|---|---|
nodeIntegration: false (default) | ✘ | ✔ |
nodeIntegration: true | ✔ | ✔ |
sandbox: true (default) | ✘ | ✔ |
sandbox: false | ✔ | ✘ |
nodeIntegration: false + sandbox: true | ✘ | ✘ |
nodeIntegration: false + sandbox: false | ✔ | ✘ |
nodeIntegration: true + sandbox: true | ✘ | ✔ |
nodeIntegration: true + sandbox: false | ✔ | ✔ |
Note: ✘ indicates a SyntaxError or ReferenceError.