簡體   English   中英

Electron: ES 模塊的 require()

[英]Electron: require() of ES Module

我第一次嘗試制作一個簡單的應用程序,但是每當我嘗試導入任何 npm package 時,我都會收到此錯誤。我不確定我做錯了什么,因為我正在使用 npm package electron-reload和那不會引發任何錯誤。

ERROR: ES 模塊的 require()

這是我的tsconfig.json:

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

這是引發錯誤的代碼:

import Hwid from "hwid";

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

最后,這是我的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"));

我正在使用 TypeScript 因為我比普通 JS 更喜歡它,我只是想知道該怎么做或者為什么這個錯誤會停止我的開發。 我希望 package 能正常運行,但沒有任何效果。

hwid是一個ESM 模塊,您的tsconfig.json指定使用 CommonJS 編寫模塊,即require() tsc (或其他編譯器)將您的import語句轉換為require()調用,ESM 不允許這樣做(因為錯誤說)。 使用可能支持 ESM 的舊版本hwid或將module更改為'es2020'並將moduleResolution設置為'node'

放一個入口索引。 cjs代碼import('./index.js')和 package.json 你已經可以添加"type": "module"

嘗試: const Hwid = require('hwid')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM