简体   繁体   中英

Electron: require() of ES Module

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.

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' .

Put an entry index. cjs with the code import('./index.js') and in package.json you can already add "type": "module" .

Try: const Hwid = require('hwid')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM