Choose appropriate module loading syntax based on your execution context and build configuration settings. Different environments have different module system capabilities that must be respected.
Choose appropriate module loading syntax based on your execution context and build configuration settings. Different environments have different module system capabilities that must be respected.
When tsconfig.json
has module: esnext
or package.json
has type: module
, import statements may not be automatically transformed to require calls. In contexts like sandboxed preload scripts that don’t support ESM, you must use require syntax instead:
// In sandboxed preload scripts, use require even with ESM config
const { ipcRenderer, contextBridge } = require('electron/renderer');
// For dynamic imports in ESM contexts, use import()
packageJson = await import(packageJsonPath, { assert: { type: 'json' } });
Always verify that your chosen loading mechanism is compatible with both your build configuration and the target execution environment. Sandboxed contexts, Node.js versions, and TypeScript compiler settings all influence which module syntax will work correctly.
Enter the URL of a public GitHub repository