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