【问题标题】:Ionic and Electron Application does not start. Error Launching App, Cannot find module at {Project Directory}离子和电子应用程序未启动。启动应用程序时出错,在 {Project Directory} 中找不到模块
【发布时间】:2019-05-09 00:57:05
【问题描述】:

我一直在尝试将离子应用程序转换为桌面应用程序,但似乎找不到解决方案。每当我运行electron . 时,我都会弹出一个说

我已将我的主脚本添加到我的 package.json 并添加了一个自定义构建命令,该命令构建我的应用程序并随后运行电子。我的 package.json 看起来像这样

{
"name": "businessapp-desktop",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
  "start": "ionic-app-scripts serve",
  "clean": "ionic-app-scripts clean",
  "build": "ionic-app-scripts build",
  "lint": "ionic-app-scripts lint",
  "electon-serve": "ionic-app-scripts build && electron ."
},
"main":"electron/electron.js",
"dependencies": {
  "@angular/animations": "5.2.11",
  "@angular/common": "5.2.11",
  "@angular/compiler": "5.2.11",
  "@angular/compiler-cli": "5.2.11",
  "@angular/core": "5.2.11",
  "@angular/forms": "5.2.11",
  "@angular/http": "5.2.11",
  "@angular/platform-browser": "5.2.11",
  "@angular/platform-browser-dynamic": "5.2.11",
  "@ionic-native/core": "~4.17.0",
  "@ionic-native/splash-screen": "~4.17.0",
  "@ionic-native/status-bar": "~4.17.0",
  "@ionic/pro": "2.0.3",
  "@ionic/storage": "2.2.0",
  "ionic-angular": "3.9.2",
  "ionicons": "3.0.0",
  "rxjs": "5.5.11",
  "sw-toolbox": "3.6.0",
  "zone.js": "0.8.26"
},
"devDependencies": {
  "@ionic/app-scripts": "3.2.1",
  "electron": "^3.0.10",
  "typescript": "~2.6.2"
},
"description": "An Ionic project"
}

而我的electron.js 看起来像这样

const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

const path = require('path');
const url = require('url');

// Keep a global reference of the window object, if you don't, the window 
will 
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 1200, height: 700});

    // and load the index.html of the app.
    const startUrl = process.env.ELECTRON_START_URL || url.format({
            pathname: path.join(__dirname, '/../www/index.html'),
            protocol: 'file:',
             slashes: true
        });
    mainWindow.loadURL(startUrl);
    // Open the DevTools.
    mainWindow.webContents.openDevTools();

    // Emitted when the window is closed.
    mainWindow.on('closed', function () {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
})
}

// 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.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
    app.quit()
}
});

app.on('activate', function () {
// On OS X 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 (mainWindow === null) {
    createWindow()
}
});

// 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.

我已经安装了电子依赖项 npm i --save-dev electron 但是每当我尝试按照教程here 中的说明运行electron . 时,都会出现上述错误。

如果能提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: node.js ionic-framework ionic3 electron


    【解决方案1】:

    终于修好了。问题是印刷的。我的应用程序无法访问 main.js,因为文件路径错误。如果您遇到类似问题,请检查您的文件路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-29
      • 2023-01-24
      • 2019-01-30
      • 2021-09-22
      • 1970-01-01
      • 2017-05-30
      • 2016-06-12
      相关资源
      最近更新 更多