Build/Package your Electron app and use it Locally.
Tags: webdev, javascript, tutorial, beginners
Published: 2020-11-27 13:40:52

Are you struggling to build your electron app and use it locally?I will solve that issue in this article.

For this purpose you can use either one of them.
- electron-builder.
- electron-packager.

But in this post I am going to use electron-packager.


After completing the coding part of your electron app with HTML,CSS and JavaScript(even React.js), npm install electron-packager as dev dependency.


```js

npm install --save-dev electron-packager

```
<strong>Remember always install electron packages as dev dependencies.</strong>

Now go to <strong>package.json</strong> and do the following,

- In scripts add the following code.

```js
"scripts": {
    "start": "electron .",
    "package-win": "electron-packager . markdownelectron  --icon=icon/language_markdown_outline_icon_139425.ico --overwrite --asar --platform=win32 --arch=ia32  --prune=true --out=buildsep --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"MarkDown\""
  },

```
This one is for windows because I use a windows machine.

<strong>For Mac users:</strong>
```js

electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds

```
<strong>For Linux users:</strong>
```js
electron-packager . electron-tutorial-app --overwrite --asar=true --platform=linux --arch=x64 --icon=assets/icons/png/1024x1024.png --prune=true --out=release-builds
```

<strong> The explanation of above code:</strong>

- We are packaging everything with <code>electron-packager . </code>
- Then We are giving your electron app a name. Mine is <strong>markdownelectron</strong>.
- Very important stuff here is the image. I got errors several times and I banged my head on the desk until I found out the answer.
 - You have to get a 512x512 image to work as icon or your icon will not show up. Get 256x256 and 1024x1024 images as well.

In your index.js add the below code for image to show up. 
 

```js
function createWindow() {
 
  const win = new BrowserWindow({
    show:false,
    icon:'./icon/language_markdown_outline_icon_139425.ico',
    webPreferences: {
      nodeIntegration: true,
    },
   
  })}
```

Make sure the icon folder is in the main folder directory with the image inside.

- Override option overrides the existing folder if you have created a build before.

- Prune removes unnecessary packages which are not required in production.

- Out means the folder name in which our built electron app will reside.

The rest are platform specific code which you can simply copy and paste in your code from above.

Now in terminal do the following steps.

```js

npm run package-win

```

The above is for windows users.

You can also do like below, if you want to build for all of them.

```js

"scripts": {
 "package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds",
"package-win": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\"",    
"package-linux": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=linux --arch=x64 --icon=assets/icons/png/1024x1024.png --prune=true --out=release-builds"
 }

```
Here You can create a build command for each one of the platforms easily by changing the code slightly.

Now in terminal you can paste the below code one by one and build your electron app for your all of the Operating Systems.

```js

npm run package-mac
npm run package-win
npm run package-linux

``` 

And that's how you build an electron app and use it locally.

<strong><u>My Personal Experience:</u></strong>

When I built my first electron app I used this website called [Christianevangel](https://www.christianengvall.se/) and it was extremely easy for me to build my own electron app.

I referred to this post by him [Package electron app](https://www.christianengvall.se/electron-packager-tutorial/) to make this one.

I wrote this in my own style but he was the one who helped me make this post.

### Thank you for reading!!

<strong> <u>Check out my portfolio: </u> [Gautham's portfolio](https://gautham-portfolio.netlify.app/)</strong>

<strong> <u> Check out my blog: </u>[coding-magnified.tech](https://coding-magnified.tech)</strong>


### <u>My Other Articles: </u>

- [Universal CSS properties everyone must know](https://dev.to/gautham495/universal-css-properties-everyone-must-use-4kie)

- [Create-react-app](https://dev.to/gautham495/creating-a-new-react-app-with-create-react-app-4e9m)

- [Git for beginners](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p)

- [Change headers in react with react-helmet](https://dev.to/gautham495/changing-headers-in-react-with-react-helmet-d0d)

- [Know How to apply box-shadow on all four sides.](https://dev.to/gautham495/know-how-to-apply-box-shadow-on-all-four-sides-43pn)
 
- [Simple CSS Selectors](https://dev.to/gautham495/simple-css-selectors-for-beginners-2i44).

- [CSS Pseudo Elements](https://dev.to/gautham495/css-pseudo-elements-for-beginners-53ob).