Run Node.JS Applications Using WSL (Bash on Windows)

Run Node.JS Applications Using WSL (Bash on Windows)

This tutorial is meant for users (developers) who are trying to run their existing Node.js applications on Windows.

For a long time, Windows users had a lot of issues being able to run Node applications on Windows, especially when dealing with very long names and installing modules that need to be compiled. This isn’t an issue of Node or Windows but the fact is, most developers were treating Windows as an afterthought. For this, Windows released a preview of Bash On Windows or WSL (Windows Subsystem Linux)

We won’t cover how to install WSL on Windows 10 and we will assume you already have access to Ubuntu Bash for Windows.

Install NVM

Launch Bash for Windows app and run:

1
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

Install Visual Studio Code for Windows

Download from here

Install GIT

1
sudo apt-get install git

Clone the application

1
git clone http://github.com/youruser/application.git

Install modules

To install node_modules you have two options:

  • You can try to run npm install right from the current folder (windows mount). But you may run into file naming issues just like on Windows, or.
  • You can symlink node_modules to your home folder (in Linux).

Example:

1
2
mkdir -p ~/node/mysite/node_modules
ln-s ~/node/mysite/node_modules /mnt/c/Users/user/Sites/mysite/node_modules

Please note that the folder you are linking from (~/node/mysite/node_modules) must be named node_modules otherwise, node won’t find them when you try to run the app.

Run the App

If everything goes well, you should be able to run node index.js (or whatever your application uses).

Debugging

Even though launching node directly Visual Studio Code isn’t working well, you can debug the application by attaching to node.

First, launch the application in debug mode.

node -debug index.js
Then in Visual Studio Code open launch.js (from the .vscode folder) and edit the Attach section

1
2
3
4
5
6
7
8
9
10
11
12
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": "/mnt/c/Users/User/Sites/mysite/"
}

remoteRoot is needed in order for breakpoints to properly work.

After this setup you should be able to run and debug applications built on Node.js

More Content