简体   繁体   English

Electron: ES 模块的 require()

[英]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.我第一次尝试制作一个简单的应用程序,但是每当我尝试导入任何 npm package 时,我都会收到此错误。我不确定我做错了什么,因为我正在使用 npm package electron-reload和那不会引发任何错误。

ERROR: require() of ES Module ERROR: ES 模块的 require()

This is my tsconfig.json:这是我的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:最后,这是我的BroswerWindow代码:

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.我正在使用 TypeScript 因为我比普通 JS 更喜欢它,我只是想知道该怎么做或者为什么这个错误会停止我的开发。 I'm expecting the package to run like normal, yet nothing works.我希望 package 能正常运行,但没有任何效果。

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). hwid是一个ESM 模块,您的tsconfig.json指定使用 CommonJS 编写模块,即require() tsc (或其他编译器)将您的import语句转换为require()调用,ESM 不允许这样做(因为错误说)。 Either use an older version of hwid which may support ESM or change module to 'es2020' and set moduleResolution to 'node' .使用可能支持 ESM 的旧版本hwid或将module更改为'es2020'并将moduleResolution设置为'node'

Put an entry index.放一个入口索引。 cjs with the code import('./index.js') and in package.json you can already add "type": "module" . cjs代码import('./index.js')和 package.json 你已经可以添加"type": "module"

Try: const Hwid = require('hwid')尝试: const Hwid = require('hwid')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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