javascript - Electron:ES模块的require()

标签 javascript typescript electron

我第一次尝试制作一个简单的应用程序,但是每当我尝试导入任何 npm 包时,我都会收到此错误。我不确定我做错了什么,因为我使用的是 npm 包 Electron-reload ,并且没有抛出任何错误。

错误: require() of ES Module

这是我的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 更喜欢它,我只是不知道该做什么或为什么这个错误停止了我的开发。我希望该包能够正常运行,但没有任何效果。

最佳答案

使用代码 import('./index.js') 放置一个条目index.cjs,并且在 package.json 中您已经可以添加 "type “:“模块”

关于javascript - Electron:ES模块的require(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71865552/

相关文章:

javascript - 在选择选项中删除几个文本

javascript - JQuery 创建按钮但不可点击

javascript - 如何从包含 undefined 而不是错误的 bool 表达式中得到 false?

node.js - Electron setLoginItemSettings 在托盘菜单应用程序上不起作用

electron - Electron 建立时间变量

reactjs - "TypeError: Cannot read property ' 带有 chrome-devtools 的伪类型 ' of undefined"

javascript - 什么情况下我们需要在javascript中实现单例类

javascript - 使用循环在 Javascript 中创建 <tr>

typescript - 有没有办法取消mongoose find执行并从redis返回数据?

multithreading - 多线程 Firebase 函数