我正在尝试创建我的第一个电子应用程序。当从终端调用npm start时,我得到这个错误:
TypeError:无法读取未定义的属性(阅读“whenReady”).
在ModuleJob.run(节点:internal/modules/esm/module_job:194:25)。
这是我的package.json文件:
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"type": "module",
"main": "main.mjs",
"scripts": {
"start": "electron ."
},
"repository": "https://github.com/Falcon-JasonM/2024_electron_sandbox.git",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^28.0.0"
}
}
字符串
这是我的主要脚本:
// Modules to control application life and create native browser window
import {magicEightBall} from './chapter3.mjs';
const { app, BrowserWindow } = import('electron')
const path = import('node:path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html');
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
{createWindow();
magicEightBall();}
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
型
我承认在尝试将第二个JS脚本chapter3.mjs导入到主.mjs文件的过程中可能会遇到一些问题。有人能告诉我正确的方法来重新连接它,以便当我在命令行中输入npm start时,两个脚本都运行,浏览器窗口启动?
1条答案
按热度按时间yacmzcpb1#
我认为问题可能在于你如何混合不同的导入方法。尝试对所有方法使用命名导入:
字符串
This question对import和const之间的区别给出了更多的解释。