This tutorial is out of date and no longer maintained.
Electron makes it easy to create cross-platform desktop applications with JavaScript. As it uses the Node.js runtime, we have the luxury of using any frontend framework we want! In this article, we’ll be looking at getting up and running with Vue and Electron to create our own applications.
To get started, run the following in your terminal:
Use the electron-vue
starter and create a my-todos
project:
You’ll then be asked some questions about your project, fill them in to generate the config file:
After filling in the above (you can switch some steps out if you want, most of it is personal preference), we have a folder named my-todos
with our project inside. Install the dependencies and then open this with your favorite editor:
Note: If you’re a Windows user, you may need to follow the steps outlined here prior to continuing.
Navigate to the directory:
Install the packages:
Run the server:
If we’ve done everything correctly at this stage, we should be greeted with a macOS application that contains information about our project:
Most of our work will be done inside of the src
directory, and I’d like to bring your attention to the main
and renderer
folders.
Main houses index.js
and index.dev.js
, files that are related to the main process. This is things such as creating a new BrowserWindow
with dimensions, listening for app-wide events, and so on.
Renderer is where we keep our Vue application and can be thought of as the directory for our frontend code.
Note: For more information on the Electron architecture and the differences between the main and renderer process, visit this page.
In order to get a feel for how Electron works, let’s visit src/main/index.js
and see how our main page is defined:
The BrowserWindow
object can be used to display a new browser window (as the name suggests), and we’re using it to open index.html
when the application is ready. In turn, this will start our Vue app, giving us the ability to hook into native desktop features.
Now that we know how the Vue application is started, let’s take a look at the defined routes within our application. Head over to src/router/index.js
:
As we can see, the LandingPage
component is defined as the default route for our application. We can therefore edit or create a new routes
object with our own components in the future.
In order to get Vuex to work with our Electron project, we’ll need to provide a path to our store within src/main/index.js
:
We can then make a new Store module named Todo.js
within src/renderer/store/modules
:
We’re using the third-party uuid
module to generate new IDs for each Todo.
Install that via npm:
Finally, we can edit our LandingPage.vue
component to include our small Todo list:
As this is a demonstration app, I’ve decided to not componentize this further. This finally gives us the following:
As we’re using electron-builder
to build our application, we can run the following:
If we look in our package.json
, we can also see that we have a variety of other commands at our disposal:
This can be further customized inside of package.json
by editing the following object:
The results of our built application can be found in the build/mac
or build/platform
folder.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!