Integrate Angular 7 with ElectronJS
Last Updated :
25 Jul, 2024
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. Electron can be combined with several powerful frameworks such as Angular 4+, AngularJS 1.x, React, etc for building complex applications and providing enhanced functionality. Electron at its core is a
NodeJS application which can interact with the native OS environment. With NodeJS integration we can access several low-level APIs which otherwise would not be accessible in a Sandbox Browser environment. With Angular integration, we gain access to several advantages and features such as MVC (Model-View-Controller) architecture, modules and custom directives. This tutorial will demonstrate how to integrate
Angular 7 with ElectronJS and also access the Electron APIs from within Angular. We assume you are familiar with the prerequisites as covered in the above-mentioned link. For Electron and Angular to work, node and npm need to be pre-installed in the system.
Note:
This tutorial is applicable to work with Angular 5+ versions as well.
Example:
Follow the given steps to integrate Angular 7 with Electron.
- Step 1: Navigate to an Empty Directory to set up the project, and run the following command,
npm install -g @angular/cli
To install the Angular CLI globally. The Angular CLI tool is used to create projects, perform tasks such as testing and deployment, and generate various components of the code. Create a new Angular project by running the following command and provide the project name of your choosing, ng new ang-electron
This command prompts you to provide information about features to include in the project. Choose the appropriate by pressing the Enter key.
This will also install the required Angular dependencies and packages into node_modules. Once done, Install Electron using npm and save it as a dev-dependency. npm install electron --save-dev
At this point, the Angular application is ready and can be served locally. To serve the application on localhost, run the following commands, cd ang-electron
ng serve

- Step 2: We will connect both the frameworks via code to launch the Electron application and make it use Angular. As with any Electron project, we need to create an entry point into the application. Create the main.js file in our base project folder. This file is going to be our entry point into the application. main.js:
javascript
const { app, BrowserWindow } = require('electron')
function createWindow () {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// Load the index.html of the app
// From the dist folder which is created
// After running the build command
win.loadFile('dist/ang-electron/index.html')
// Open the DevTools.
win.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.
// This method is equivalent to 'app.on('ready', function())'
app.whenReady().then(createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS 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', () => {
// 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()
}
})
// 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.
For the Boilerplate code of the main.js file, Refer this link. We have modified the code to suit our project needs. Once we run the build command, it is going to create a distribution of our angular project in the dist folder. We are going to refer the index.html file from this folder. - Step 3: A small change is required in the index.html file located in the src folder. Replace the following code.
<base href="/">
with <'base href="./">
This change is important otherwise it won’t be able to find and refer the necessary files required to run the application from dist folder. We also need to make a few changes to the package.json file.package.json:{
"name": "ang-electron",
"version": "0.0.0",
"main": "main.js",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"electron": "ng build && electron ."
},
// ...
We have specified the main.js file as required by Electron. We have also introduced a new custom electron command in the scripts tag for building and launching the application. The ng build command is used for building the Angular application and deploying the build artifacts. It writes generated build artifacts to the output folder. By default, the output folder is dist/. Output: Once the respective changes are done, we are ready to launch the Electron application. To launch the application, run the command, npm run electron

- Step 4: At this point, we have successfully integrated Angular with Electron. To use the Electron APIs within Angular, we can follow any of the two approaches:
Similar Reads
Introduction to ElectronJS
Creating cross-platform applications often requires learning multiple programming languages and frameworks. However, Electron.js eliminates this complexity by allowing developers to build desktop applications using familiar web technologies like JavaScript, HTML, and CSS. In this article, we will le
5 min read
How to Create a Desktop App Using JavaScript?
Building a JavaScript desktop application is possible using various frameworks and technologies. One popular approach is to use Electron, which is an open-source framework developed by GitHub. Electron allows you to build cross-platform desktop applications using web technologies such as HTML, CSS,
2 min read
Environment Variables in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
7 min read
Manage Staging Environments in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
6 min read
Integrate Angular 7 with ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
8 min read
Command Line Arguments in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
6 min read
Keyboard Shortcuts in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.U
6 min read
Windows Taskbar Operations in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
6 min read
File Upload in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
8 min read
Managing Themes in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
9 min read