Visual Studio Code Electron

  

Electron at that time needed street credibility and exposure as a new web development tool. It is a hybrid tool that grants web developers the ability to create desktop applications in a web environment, with ease of use. Microsoft was happy to champion their cause to get more exposure in web development circles. WPF and UWP are good enough. Electron has a launch flag -remote-debugging-port, which lets you specify a port for remote debugging. The language spoken at this port is Chrome Debugging Protocol, and VS Code has an extension that just handles that: Debugger for Chrome.

Want an easier way? Read on for how to debug shared code using Electron, Visual Studio Code and VS Live Share.

TLDR; Share an Electron session using Live Share

Here's a quick synopsis of getting a Visual Studio Live Share session working with Electron.

  1. Setup two Electron development environments on different machines
  2. Install VS Code and the Visual Studio Live Share extension
  3. In the main environment (Environment A), start a live share session and join it from Environment B.
  4. In Environment B, modify the package.json file to launch the node web server on port 4201 (or something other than the default 4200). This should be done on the local code base of Environment B, not the code base being shared in the live share session.
  5. Run npm start on Environment A
  6. Run npm start on Environment B on its local code base, not the code being shared in the live share session.

Now any participant in the live share can reload the rendered page in the Electron app and see the changes being performed in the live share.

Note: This does not actually share debugging breakpoints within VS Code, but it does allow everyone in the live share session to run the app locally with any changes made during the session.

Overview

Recently, I was on a very interesting project with a coworker that we were building using Electron and Angular 7. This was the first time I had worked with Electron and I was pleasantly surprised how efficient my workflow became. For the first time in a long time, I was able to completely develop all of the system components on my iMac Pro without having to constantly spin up different virtual machines for different components.

When running the Electron app locally, I was able to make modifications and instantly see the changes upon saving a code file using Visual Studio Code. I was able to run and debug the .NET Core api code in Visual Studio Code. I was able to run and debug the Angular web app in Visual Studio Code. Even better, I was able to collaborate with my coworker using Visual Studio Live Share. The Live Share team is constantly adding new features, and one really cool one is shared debugging of web apps. When running a web app in Visual Studio Code, Live Share is able to create a tunnel between the collaborators' machines and allow a shared debugging session to be set up, complete with the web app running on both machines! Needless to say when working in a distributed environment, this kind of tooling greatly increases the capacity for development collaboration and pair programming. After a few debug sessions, my coworker and I stumbled upon an even cooler method of utilizing Live Share and Electron...shared debugging! Well, kind of.

Setting up your environment

First things first, before diving into the details, let's make sure your environments are set up properly.

Install Node.js

I recommend having the latest version of Node from the Node download page. In this article, I tested with 11.10.0 installed.

Get the code

If you want a really quick demo, you can grab the code I use from maximegris's github which is actively being maintained. I also have a somewhat up to date fork here since I wanted to make sure I (and thus you) have a fork. The code will be needed on both environments even though you technically only need it on the host for Visual Studio Live Share. This is due to a trick I use in order to make this whole thing work, which I will explain later.

Install Visual Studio Code and the Visual Studio Live Share extension

You will want the latest versions for your operating system, which you can download from the VS Code download page. I haven't tested this on Linux, but in theory it should still work. Once you have Visual Studio Code installed, install the VS Live Share extension from the Extensions pane in VS Code.

That's it! That's all you need in order to build and run your Electron + Angular 7 app. So now, let's jump right in and get to that.

Running the app

Assuming you're using the same template that I am using in this example, getting started is very easy. There are only a few steps:

  1. Open the folder containing the code in VS Code.
  2. Run npm install
  3. Run npm start

You should now have a copy of the template Electron app running on your machine!

But what about Visual Studio Live Share?

Now that you've got your newly minted Electron app running, the first thing you want to do is show it off to a coworker and let them help you modify it, right!? Well let's get to it! The first step is to create a VS Live Share session. This is very simple and is done by clicking on the Live Share button on the bottom toolbar as shown.

It is simple enough for other people to join, just give them a url that will launch VS Code and join the session when they visit it.

Note: A collaborator does NOT normally need a copy of the code on their local machine in order to join a session. A guest to a live share session will have a copy of the source tree in a temporary folder. In our case, we need a local copy only because we are going to trick the locally running application into loading its content from the host's system. More details on that to follow.

Studio

Running the Electron app on the host and guest machines

This is where things go off the normal beaten path. My coworker and I wanted to be able to both run an instance of the Electron app with the code on which we were collaborating. The first thing we tried was simply running the app on both machines using npm start. This worked fine on the host, but after the app was running on the host, the electron app on the guest wouldn't launch due to an error concerning a port being in use.

> [email protected] ng:serve /home/parallels/development/angular-electron
> ng serve
Port 4200 is already in use. Use '--port' to specify a different port.

After scratching our heads for a few moments, we realized that this was due to VS Live Share setting up a local tunnel from the host machine to the guest on port 4200. It does this when you launch a node web server using npm start. VS Live Share is assuming you want to be able to load the website data on all of the collaborators' machines.

This is an awesome feature of VS Live Share. It isn't limited to just sharing the node server launched with npm start. You could even use it to share a database instance on the host machine, or an api web app running on a different port, but I digress. The point is, when running the Electron app using npm start on the host machine, VS Live Share automatically sets up a tunnel for you so that your collaborators can access the renderer content that the Electron app will load. Now the only trick is to get run the Electron app on the guest machines without the port error we saw above.

Running the Electron app on guest machines, take two

After a moment of thought, we tried a slight tweak to see if we could get the Electron app running on the guest machine while loading the code on which we were collaborating. And it worked!

On the guest machine, simply modify your package.json to launch the node web server on a different port. It's extremely simple, and is done by changing two lines of code:

'ng:serve': 'ng serve'to 'ng:serve': 'ng serve --port 4201'

- and -

'electron:serve': 'wait-on http-get://localhost:4200/ && npm run electron:serve-tsc && electron . --serve' to 'electron:serve': 'wait-on http-get://localhost:4201/ && npm run electron:serve-tsc && electron . --serve'

Save the changes to the package.json on the local copy of the source on the guests, not the package.json that is shared in the VS Live Share session. These changes are temporary as well and should not be checked into source control.

Once these changes are done, simply run npm start on the local copy of the source in a terminal or console. The Electron app should now run on each of the guest machines as well! What's more is that while the node web server is running on 4201 on the guest machines, the Electron app is still requesting its content from port 4200! This is set in one of the main.ts files and we didn't modify that.

The end result is that you can now share a VS Live Share collaboration session with many participants, run the Electron app on the host, run the Electron app on the guest machines, and then all participants will see any changes being made to the app...LIVE! See the proof below if you don't believe me.

As you can see, I have a collaboration session running with three participants; my macOS, one Ubuntu VM, and one Windows VM. I'm editing code from VS Code running on my mac, saving the edited code from VS Code running in the Windows VM and watching all of the Electron apps immediately update on all three environments.

This workflow was incredibly helpful when working with my colleague remotely on this project. I hope it helps you as well until this is natively supported in LiveShare itself.

Visual

Visual Studio Code (VSCode) is by far my favourite editor. I've got nothing against Sublime or Atom, but the first one is not free and the second one tends to get a bit slow. I use all 3, in case you think of accusing me of favouritism, but my Go-To editor is VSCode. One of its biggest strengths is obviously the debugging experience.

This week I decided to start rewriting my password application (PasswordDefence) with Electron. This will mean that once finalized, I will have a mobile and desktop presence and it also gives me a good opportunity to play around with a couple of new frameworks.

Visual Studio Code For Beginners

What is Electron?

Electron (formerly known as Atom) is a framework that runs on top of node.js and allows you to create beautiful desktop applications using web technologies. The HTML, CSS and JavaScript that you got to love over the years, can now be used to write some really solid and robust desktop application for the Mac and Windows ecosystems. GitHub and Slack are both apps written with Electron. On top of that, you can use Flux, React etc to enhance your application and get to your 'destination' faster.

Environment Setup

To work with Electron, you need to install it. Fire up a node command prompt and type the following:

With the binaries installed, we can start coding. I initially wrote my code in Atom. Atom works fine and doesn't require any special setup. You do, however, have to use the the node console to launch the application by navigating to your app directory and typing electron .

Setup VSCode to work with Electron

Open your application folder in VSCode. Press Ctrl+Shift+B to build the project. This will prompt you to setup a tasks.json, which in effect tells VSCode what to do when you issue this command. Comment all the tasks out and add the following settings:

The next time you issue a Ctrl+Shift+B command, you should get a nice build and have your application launched.

Debugging Electron with VSCode

VSCode takes the development experience one step further by allowing you to debug your code. Go to the debugger window and click on settings. This will prompt you to create a launch.json. Accept it and open the file. Paste the following settings in it:

Make sure you point to the right entry JavaScript file for your application, if it's different than mine.

Visual Studio Code Without Electron

These 2 settings should be enough to get you started. After that, it's all fun working with the same tools and frameworks you already know to create really cool applications. Let me know in the comments if you have any problems with this setup.