Introduction
In this article, we will learn how to build Windows apps from non-Windows platforms using Angular 7 with Electron.
Prerequisites
- Node
- Npm
- Angular CLI
- TypeScript
- Visual Studio Code
- Electron
- Electron Packager
Description
We can create desktop applications with JavaScript by providing a Node.js runtime with native operating system APIs with the help of Electron. Electron implements web pages as its GUI which is controlled by JavaScript. Electron application is a Node.js application.
Note
Before going through this session, please visit my previous sessions related to Angular 7 application.
These are the steps to be followed for setting up Electron and then to create an Angular 7 desktop app with Electron Packager.
Step 1
We should install Electron. The recommended way to install it is as a development dependency in your app, which allows us to implement on various apps with different Electron versions. To do so, run the following command from your app's directory.
npm install --save-dev electron
Then, after successful installation of Electron, we can see the Electron version, as shown below.
Step 2
Then, let us create a JavaScript file called "main.js" file, as shown below.
Next, we should put some code here to create a Windows app and handle all the system events of your application. We can see the code described in the comments.
- const { app, BrowserWindow } = require('electron')
-
-
-
- let win
-
- function createWindow () {
-
- win = new BrowserWindow({ width: 800, height: 600 })
-
-
- win.loadFile('dist/angular7app/index.html')
-
-
- win.webContents.openDevTools()
-
-
- win.on('closed', () => {
-
-
-
- win = null
- })
- }
-
-
-
-
- app.on('ready', createWindow)
-
-
- app.on('window-all-closed', () => {
-
-
- if (process.platform !== 'darwin') {
- app.quit()
- }
- })
-
- app.on('activate', () => {
-
-
- if (win === null) {
- createWindow()
- }
- })
-
-
-
Then, modify the code in the href section of index.html file.
- <meta charset="utf-8">
- <title>Satyaprakash-Angular Developer</title>
- <base href="./">
Step 3
Open the package.json file and add below file reference as mentioned below.
- main.js
- electron (inside scripts section)
Then, we need to run the following command from our app's directory to show the output of the desktop app. Here, "ng build && electron ." means build the app, then run Electron.
We can run the Angular app as a native desktop app with the following command.
npm run electron
OUTPUT
Here, the Windows desktop app is shown below.
Step 4
Deploying Windows desktop applications using Electron Packager
Electron Packager will package your Electron app into OS-specific bundles via JavaScript or the command line. Electron Packager is a command line tool and Node.js library that bundles Electron-based application source code with a renamed Electron executable and supporting files into folders ready for distribution. It is is an open source project.
It supports the below platforms to run,
- Windows (32/64 bit)
- OS X (also known as macOS)
- Linux (x86/x86_64)
Then, we run the following command from our app's directory to install Electron Packager.
npm install electron-packager --save-dev
I have added the following command with pack name in the package.json file.
electron-packager
Then, we need to run the following command from our app's directory to deploy our desktop app in the given directory.
npm run pack
OUTPUT
Visit the directory as mentioned in the above image file.
Click on the app icon and see the output. You can drag and put this app (.exe) anywhere you want.
Summary
In this write-up, we have learned:
- Introduction to Electron and Electron Packager
- How to set up Electron and Electron Packager
- How to run a desktop app using Electron
- How to deploy desktop app (.exe) using Electron Packager