{"id":1627,"date":"2018-04-09T18:38:00","date_gmt":"2018-04-09T18:38:00","guid":{"rendered":"https:\/\/xplicity.com\/?p=1627"},"modified":"2025-01-03T14:44:20","modified_gmt":"2025-01-03T11:44:20","slug":"part-1-creating-a-desktop-application-with-electron-angular-and-typescript-basic-setup","status":"publish","type":"post","link":"https:\/\/xplicity.com\/part-1-creating-a-desktop-application-with-electron-angular-and-typescript-basic-setup\/","title":{"rendered":"Part 1: Creating a desktop application with Electron, Angular and TypeScript: Basic setup"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Despite the fact that more and more applications are being moved to the web, there is a still high demand of developing desktop applications. That is because web applications still have some limitations (e.g., file system access, a separate window with its taskbar and system tray icons) and cannot replace desktop applications completely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Electron framework allows you to create a cross-platform (Windows, MacOS, Linux) desktop applications with conventional web technologies \u2013 HTML, JavaScript and CSS. Under the hood, it uses Chromium browser\u2019s engine and Node.js runtime. So, if you are familiar with web technologies and want to create a desktop application or even if you already have a web application, but want to have a desktop version of it, Electron enables you to do that efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since we can use standard web technologies for developing a desktop Electron app, why not use Angular instead of plain HTML, JavaScript, and CSS? That way we will benefit from Angular pros like enhanced HTML (conditions, loops, variables), data binding and reusable components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, you will learn how to create and configure a new desktop application with Angular and Electron.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">INSTALL PREREQUISITES<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To be able to create Electron-Angular application you need to have Node.js and&nbsp;npm&nbsp;installed on your computer. You can download Node.js from&nbsp;<a href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. I recommend to update Node.js&nbsp;and&nbsp;npm&nbsp;to the latest version even if you already have them installed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The command to update&nbsp;npm&nbsp;to the latest version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm&nbsp;install&nbsp;-g&nbsp;npm<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Install&nbsp;(or update)&nbsp;Angular CLI globally:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm&nbsp;install -g @angular\/cli<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE A NEW ANGULAR APPLICATION<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new Angular application using Angular CLI.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng new ng-electron-app<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace&nbsp;<em><strong>ng-electron-app<\/strong><\/em>&nbsp;with your application name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MODIFY INDEX.HTML<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Modify&nbsp;<em><strong>base&nbsp;href<\/strong><\/em>&nbsp;in the&nbsp;<em><strong>src\/index.html<\/strong><\/em>&nbsp;file&nbsp;\u2013 replace \u201c\/\u201d by&nbsp;\u201c.\/\u201d. Since we are developing a desktop app,&nbsp;we&nbsp;are&nbsp;working&nbsp;in&nbsp;the&nbsp;local&nbsp;file system. Therefore,&nbsp;we need to use&nbsp;\u201c.\/\u201d,&nbsp;which means that Angular will look for&nbsp;resource files in the application root directory.&nbsp;If we don\u2019t change&nbsp;the base&nbsp;href&nbsp;Angular will try to look for&nbsp;the&nbsp;resource files in the root folder&nbsp;of the disk&nbsp;(in Windows OS case&nbsp;this is usually \u201cc:\\\u201d).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;base href=\".\/\"&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Modify&nbsp;the&nbsp;title&nbsp;too. This will be&nbsp;the title of the application&nbsp;window.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"utf-8\"&gt;\n  &lt;title&gt;Angular Electron App&lt;\/title&gt;\n  &lt;base href=\".\/\"&gt;\n\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n  &lt;link rel=\"icon\" type=\"image\/x-icon\" href=\"favicon.ico\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;app-root&gt;&lt;\/app-root&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CHECK IF IT WORKS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run&nbsp;<em><strong>ng build<\/strong><\/em>&nbsp;command in the root directory of your app.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng build<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"174\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1df7cddb5.png\" alt=\"\" class=\"wp-image-1629\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1df7cddb5.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1df7cddb5-300x87.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Open&nbsp;<em><strong>dist\/index.html<\/strong><\/em>&nbsp;in your browser. If everything is fine you should see something like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"493\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0051e25.png\" alt=\"\" class=\"wp-image-1630\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0051e25.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0051e25-300x247.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">INSTALL ELECTRON PACKAGE<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Install electron npm package locally. To do that in the root directory of the project run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install electron@latest --save-dev<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"206\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0c24cf6.png\" alt=\"\" class=\"wp-image-1631\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0c24cf6.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e0c24cf6-300x103.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em><strong>\u2013save-dev<\/strong><\/em>&nbsp;flag adds the electron dependency to the&nbsp;<em><strong>devDependecies<\/strong><\/em>&nbsp;array in the&nbsp;<em><strong>package.json<\/strong><\/em>&nbsp;file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE ELECTRON\u2019S MAIN FILE<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the root of your application create folder named&nbsp;<em><strong>electron<\/strong><\/em>. In this folder create&nbsp;<em><strong>main.ts<\/strong><\/em>&nbsp;file. This file the entry point of the Electron application. This file is responsible for managing application\u2019s windows and handling of system events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can copy the&nbsp;<em><strong>main.ts<\/strong><\/em>&nbsp;file contents from the Electron Quick Start Git repository:<br><a href=\"https:\/\/github.com\/electron\/electron-quick-start-typescript\/blob\/master\/src\/main.ts\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/electron\/electron-quick-start-typescript\/blob\/master\/src\/main.ts<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After compilation (do not compile anything yet!) of the app both&nbsp;<em><strong>main.js<\/strong><\/em>&nbsp;(compiled&nbsp;<em><strong>main.ts<\/strong><\/em>) and&nbsp;<em><strong>index.html<\/strong><\/em>&nbsp;file will be in the same directory \u2013&nbsp;<em><strong>dist<\/strong><\/em>, therefore we need to modify the path to the&nbsp;<em><strong>index.html<\/strong><\/em>&nbsp;in the just created&nbsp;<em><strong>main.ts<\/strong><\/em>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pathname: path.join(__dirname, 'index.html'),<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of the&nbsp;<em><strong>electron\/main.ts<\/strong><\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { app, BrowserWindow } from 'electron';\nimport * as path from 'path';\nimport * as url from 'url';\n\nlet mainWindow: Electron.BrowserWindow;\n\nfunction createWindow() {\n  \/\/ Create the browser window.\n  mainWindow = new BrowserWindow({\n    height: 600,\n    width: 800,\n  });\n\n  \/\/ and load the index.html of the app.\n  mainWindow.loadURL(url.format({\n      pathname: path.join(__dirname, 'index.html'),\n      protocol: 'file:',\n      slashes: true,\n  }));\n\n  \/\/ Open the DevTools.\n  mainWindow.webContents.openDevTools();\n\n  \/\/ Emitted when the window is closed.\n  mainWindow.on('closed', () =&gt; {\n    \/\/ Dereference the window object, usually you would store windows\n    \/\/ in an array if your app supports multi windows, this is the time\n    \/\/ when you should delete the corresponding element.\n    mainWindow = null;\n  });\n}\n\n\/\/ This method will be called when Electron has finished\n\/\/ initialization and is ready to create browser windows.\n\/\/ Some APIs can only be used after this event occurs.\napp.on('ready', createWindow);\n\n\/\/ Quit when all windows are closed.\napp.on('window-all-closed', () =&gt; {\n  \/\/ On OS X it is common for applications and their menu bar\n  \/\/ to stay active until the user quits explicitly with Cmd + Q\n  if (process.platform !== 'darwin') {\n    app.quit();\n  }\n});\n\napp.on('activate', () =&gt; {\n  \/\/ On OS X it's common to re-create a window in the app when the\n  \/\/ dock icon is clicked and there are no other windows open.\n  if (mainWindow === null) {\n    createWindow();\n  }\n});\n\n\/\/ In this file you can include the rest of your app's specific main process\n\/\/ code. You can also put them in separate files and require them here.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ADD ELECTRON BUILD COMMANDS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We need a convenient way to compile both Angular and Electron applications and then run our Electron application. We can do this by appending scripts section of the package.json file with&nbsp;<strong><em>electron-build<\/em><\/strong>&nbsp;and&nbsp;<strong><em>electron-build-prod<\/em><\/strong>&nbsp;commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\": {\n    \"ng\": \"ng\",\n    \"start\": \"ng serve\",\n    \"build\": \"ng build --prod\",\n    \"test\": \"ng test\",\n    \"lint\": \"ng lint\",\n    \"e2e\": \"ng e2e\",\n    \"electron-build\": \"ng build &amp;&amp; tsc electron\/main.ts --outDir dist -t es5 &amp;&amp; electron .\/dist\/main.js\",\n    \"electron-build-prod\": \"ng build --prod &amp;&amp; tsc electron\/main.ts --outDir dist -t es5 &amp;&amp; electron .\/dist\/main.js\"\n  },<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The only difference between these commands is that&nbsp;<em><strong>electron-build-prod<\/strong><\/em>&nbsp;command uses&nbsp;<em><strong>ng build \u2013prod<\/strong><\/em>&nbsp;command to compile the Angular app. The production build does stuff like bundling, minification, uglification, etc. and it takes longer time to compile the app. I recommend to use it only when needed. The&nbsp;<em><strong>electron-build<\/strong><\/em>&nbsp;command will compile the app much faster and is sufficient for the quick checking of the changes you\u2019ve made.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">RUN THE APPLICATION<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run&nbsp;<em><strong>electron-build<\/strong><\/em>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run electron-build<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After compilation is finished, the application should start.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"452\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e1b624df.png\" alt=\"\" class=\"wp-image-1632\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e1b624df.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e1b624df-300x226.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CONCLUSION<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We have successfully created and configured Electron-Angular desktop application. From this point, you can add new components, services, configure application menus, etc. In the\u00a0<a href=\"https:\/\/xplicity.com\/part-2-creating-a-desktop-application-with-electron-angular-and-typescript-components-services-node-js-modules\/\" data-type=\"post\" data-id=\"1619\" target=\"_blank\" rel=\"noreferrer noopener\">next article<\/a>\u00a0we will learn how to access the file system using Node.js from your Angular app.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Despite the fact that more and more applications are being moved to the web, there is a still high demand of developing desktop applications. That is because web applications still have some limitations (e.g., file system access, a separate window with its taskbar and system tray icons) and cannot replace desktop applications completely. Electron framework [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1628,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/comments?post=1627"}],"version-history":[{"count":2,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1627\/revisions"}],"predecessor-version":[{"id":2503,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1627\/revisions\/2503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/media\/1628"}],"wp:attachment":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/media?parent=1627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/categories?post=1627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/tags?post=1627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}