Embracing Multi-Platform Development with Node.js

As a Node.js developer, you’re likely no stranger to the importance of multi-platform support. Whether you’re working on Mac, Linux, or Windows, the experience should be seamless. However, subtle differences between each platform can arise, especially when developing applications that interact closely with the underlying operating system. So, how can you ensure that your code runs smoothly on your host Linux server?

Comparing Hyper-V Virtual Machines and Windows Subsystem for Linux 2 (WSL2)

In this article, we’ll explore two options for Windows developers who want to run and test their Node.js applications on Linux: Hyper-V virtual machines and WSL2. We’ll set up an instance of Ubuntu 19 with Node 12 running in both systems, compare differences between the two, and determine which system is best suited for each scenario.

The Sample App: Image Compressor

To thoroughly test the developer experience, we’ll use the prebuilt Image Compressor sample application. This simple Node.js web application accepts one or more image file uploads, compresses the images, and allows users to download the compressed images. The app interacts with the underlying operating system’s file system and requires some processing power to perform image compression.

Ubuntu on Hyper-V

To set up a Node 12 server on Ubuntu 19 running in a Windows Hyper-V virtual machine, your system must meet certain requirements, including running Windows 10 Pro edition or higher and meeting the hardware requirements set by Microsoft. We’ll use the Quick Create function in Hyper-V manager to set up an Ubuntu 19 virtual machine with minimal fuss.

Ubuntu on WSL2

WSL2 is considered an overhaul of the original Windows Subsystem for Linux. To set up Ubuntu on WSL2, we’ll need to enable a few optional features, including WSL2 itself. We’ll then install the Ubuntu distribution from the Microsoft Store and configure the system to run under WSL2.

Getting the Sample App Running

Once we have our Ubuntu systems set up, we’ll update the system, install necessary software, download, and run the sample application. We’ll use the command-line to complete these tasks and take note of the virtual machine’s IP address to access the web app directly from within Windows.

Comparing Hyper-V to WSL2

The biggest difference between running Ubuntu Linux in a Hyper-V virtual machine versus running the operating system in WSL2 lies in the ability to access the Ubuntu user interface in Hyper-V. WSL2 may have the upper hand in terms of speed and ease of use, but Hyper-V virtual machines offer more flexibility and control.

A Continuous Deployment Setup

If you intend to develop in Windows and run your app in Linux, you may want to set up a continuous deployment pattern in which you push your code to a remote Git repo from Windows and automatically pull and deploy these changes on your Linux system.

Summary of Differences

Let’s summarize the differences between Hyper-V virtual machines and WSL2 from the perspective of a Node.js developer:

  • Performance: WSL2 consistently outperforms Hyper-V in terms of file-system performance.
  • GUI Access: Hyper-V virtual machines offer a full Linux OS with a GUI shell, while WSL2 instances are command-line only.
  • Distribution Support: Hyper-V supports a wider range of Linux distributions, while WSL2 currently supports Ubuntu, SUSE Linux Enterprise Server, Debian, and Kali.

What Should I Use?

Are you more interested in running and testing your app in one or more Linux environments? WSL2 is probably the way to go. Are you looking to develop, test, and run your app directly in the Linux operating system? Hyper-V is probably the way to go.

Conclusion

Hyper-V virtual machines and WSL2 are both powerful tools for developers. While there will always be a place for Hyper-V virtual machines, the speed at which WSL2 is evolving is particularly interesting. Node.js developers working with Windows and Linux systems should keep their eyes on WSL2 and take note of its potential.

Leave a Reply

Your email address will not be published. Required fields are marked *