最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron: require() of ES Module - Stack Overflow

programmeradmin2浏览0评论

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:

{
  "pilerOptions": {
    "target": "ES5",                                 
    "module": "CommonJS",                              
    "outDir": "./app/js/",                                   
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,
    "strict": true,                                      
    "skipLibCheck": true,                                
  },
  "exclude": ["./app/js/**/*.js"],
  "pileOnSave": 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.

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:

{
  "pilerOptions": {
    "target": "ES5",                                 
    "module": "CommonJS",                              
    "outDir": "./app/js/",                                   
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,
    "strict": true,                                      
    "skipLibCheck": true,                                
  },
  "exclude": ["./app/js/**/*.js"],
  "pileOnSave": 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.

Share Improve this question asked Apr 14, 2022 at 1:42 staticstatic 271 gold badge2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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

hwid is an ESM module and your tsconfig.json specifies to write modules using CommonJS, which is the require() function. tsc (or other pilers) 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'.

Try: const Hwid = require('hwid')

You may want to check that your bundler (Webpack, Vite, etc.) works correctly, or start using one if you aren’t already.

Further reading

  • This ment on Electron issue #21457
发布评论

评论列表(0)

  1. 暂无评论