Home > other >  Electron: require() of ES Module
Electron: require() of ES Module

Time:04-14

I'm trying to make a simple app for the first time, However I keep getting this error whenever I try to import any npm package. I'm unsure of what I did wrong because I'm using the npm package electron-reload and that's not throwing any errors.

ERROR: require() of ES Module

This is my tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",                                 
    "module": "CommonJS",                              
    "outDir": "./app/js/",                                   
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,
    "strict": true,                                      
    "skipLibCheck": true,                                
  },
  "exclude": ["./app/js/**/*.js"],
  "compileOnSave": true
}

This is the code in which the error is being thrown:

import Hwid from "hwid";

ipcMain.on("get-hwid", (event) => {
    console.log(Hwid());
});

And lastly, this is my BroswerWindow code:

const window = new BrowserWindow({
        width: 700,
        frame: false,
        height: 700,
        resizable: false,
        transparent: true,
        roundedCorners: true,
        icon: path.join(__dirname, "../design/imgs/dully_logo.png"),
        webPreferences: {
            contextIsolation: false,
            nodeIntegration: true,
            preload: path.join(__dirname, "preload.js"),
            devTools: false,
        },
    });
    window.loadFile(path.join(__dirname, "../design/index.html"));

I'm using TypeScript because I prefer it more than regular JS, I'm just stuck on what to do or why this error stops my development. I'm expecting the package to run like normal, yet nothing works.

CodePudding user response:

hwid is an ESM module and your tsconfig.json specifies to write modules using CommonJS, which is the require() function. tsc (or other compilers) converts your import statements to require() calls, which is not permitted with ESM (as the error says). Either use an older version of hwid which may support ESM or change module to 'es2020' and set moduleResolution to 'node'.

CodePudding user response:

Try: const Hwid = require('hwid')

  • Related