DEV Community

Cover image for Move NodeJS server from Windows to Linux VPS
Andrej Kirejeŭ
Andrej Kirejeŭ

Posted on • Edited on

Move NodeJS server from Windows to Linux VPS

Recently, we experienced a crash of hard drive in one of our server. Thanks to the RAID, data weren't lost, but this incident was a great impetus to move our NodeJS-based chatbots to the VPS server of a reliable cloud provider. As there is always a lack of time, the challenge was set to fit in one hour. The task was exacerbated by me being totally Windows guy, with only basic knowledge of Linux. I need to google almost every command. Even ls. I'm not joking.

The first step was to set up NodeJS. Obvious apt install nodejs got me stable but ancient version 12.x. I turned to the internet and learned about Snapcraft application store. There is a snap command to install NodeJS, which turned out to be a costly mistake, as I had lost half of the allotted time just to realize that snap somehow breaks the ability of PM2 to span new processes.

Eventually, I had found the command to download and install the latest version. Quite unpronounceable, though, from a Windows perspective:

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt install nodejs
Enter fullscreen mode Exit fullscreen mode

Then came the Yarn, our package manager of choice:

npm install -g yarn
Enter fullscreen mode Exit fullscreen mode

The whole folder of the chatbot was zipped (excluding node_modules), transferred via FTP and unpacked. A bit of correction was needed to replace system environment variables with dotenv package and .env text file.

As Windows and Linux line endings differ, git was told to ignore it:

git config --global core.autocrlf true
Enter fullscreen mode Exit fullscreen mode

On Windows, we used system Task Scheduler for launching chatbot server at system startup. Here, PM2 process manager comes to the help.

npm install -g pm2 
Enter fullscreen mode Exit fullscreen mode

I tried to compel it working with yarn start command, but quickly abandoned futile attempts and retreated to calling compiled JS instead.

pm2 start dist/server.js --name chat-bot --watch
Enter fullscreen mode Exit fullscreen mode

Strange, that of two NodeJS servers, one refused to work with --watch flag. I didn't have enough time to dive into the problem and just switched off watching for the project. It is not updated often, anyway.

Don't forget to start up projects automatically upon VPS reboot, as it could happen at any moment:

pm2 startup
Enter fullscreen mode Exit fullscreen mode

That is it. Transferring NodeJS servers from Windows to Linux turned out to be not so difficult and long.

Top comments (4)

Collapse
 
chrisjust profile image
Chris Justesen

I can't help but wonder:

Why not just choose a serverless setup?
Or docker so you can ensure builds are reproducible.

To be honest I would encourage you to look into terraform rather than setting this up by hand.

Collapse
 
andreik profile image
Andrej Kirejeŭ

I think dockers will be the next step. The decision was made to be as close to bare metal as possible, as I'm new to Linux.

Collapse
 
chrisjust profile image
Chris Justesen

I get that.

However I believe you'll achieve everything you want by going with the container route.

Given i know nothing about your software nor anything about your infrastructure so my comment is as such.

If the vps is running only one application I would go that route with an always on cloudrun instance.

Thread Thread
 
andreik profile image
Andrej Kirejeŭ

Thanks for the advice. I will try dockers as soon as squeeze some time for it ))