![]() |
VOOZH | about |
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
NodeJSapplication 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 7with 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.
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. 👁 Default Optionsnpm 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👁 Angular Output<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👁 Imagenpm install ngx-electron --save Once it is successfully installed, we will import it in our app.module.ts file for it to be used throughout the application. app.module.ts:For the list of Electron APIs supported by this package, Refer to this https://www.npmjs.com/package/ngx-electron#properties. We will use the Electron shell API from the ngx-electron package. app.component.html:The Click here to Access GeeksForGeeks button does not have any functionality associated with it. To change this, make the following changes to the app.component.ts file. app.component.ts:The ElectronService exposes all Electron APIs accessible from within the Renderer Process. We will create an Instance of the ElectronService in the constructor through Dependency Injection. Output:ng generate service elec --skipTests=true The --skipTests=true does not create the spec.ts Test files for new services. This command will generate a new elec.service.ts file within the src/app folder. In this file, we will declare and add all Electron Imports which can then be used throughout the application. We will use the Electron shell API from the Main Process. elec.service.ts:The any keyword is for Type Assertion on the window object. Converting this Object using any indicates that you are no longer bound by the compiler to the default properties of the window object. This is used to prevent compile-time type errors when using Electron modules. If type-casting is ignored on the windows object, we will receive the following error: ERROR in ./node_modules/electron/index.js
Module not found: Error: Can't resolve 'fs' The typeof operator returns the data type of its operand in the form of a string. In this case, the operand is the shell module of Electron. Using this approach gives us access to all Electron APIs throughout the Application. To use this service, add the following to the app.component.ts file. app.component.ts:Output: