类型错误:无法读取未定义的属性(读取“whenReady”)

问题描述 投票:0回答:1

我正在尝试创建我的第一个电子应用程序。从终端调用 npm start 时,出现此错误:

类型错误:无法读取未定义的属性(读取“whenReady”)...

在 ModuleJob.run(节点:内部/模块/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 导入到 main.mjs 文件的过程中可能会出现一些交叉。谁能告诉我重新连接它的正确方法,以便当我在命令行中输入 npm start 时运行两个脚本并启动浏览器窗口?

javascript node.js npm electron node-modules
1个回答
0
投票

我认为问题可能在于如何混合不同的导入方式。尝试对所有这些都使用命名导入:

import { magicEightBall } from './chapter3.mjs';
import { app, BrowserWindow } from 'electron';
import path from 'node:path';

这个问题对 import 和 const 之间的区别给出了更多解释。

© www.soinside.com 2019 - 2024. All rights reserved.