Installing Node.js and NPM on a VPS Hosting Server

1. Access the Remote Server via SSH

            
ssh -p PORT USERNAME@HOSTIP # Example: ssh -p 22 raj@216.32.44.12

2. Verify if Node.js and NPM are Installed

            
node -v npm -v

3. Install Node.js and NPM

            
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\ sudo apt-get install -y nodejs

4. Get the Latest Node.js and NPM Installation Instructions

For the most recent installation instructions, refer to the official documentation:

NodeSource Distributions

5. Confirm Successful Installation

            
node -v npm -v

6. Update NPM (If Desired)

            
npm install -g npm@latest

7. Clean Up Unused Packages (Optional)

            
sudo apt-get clean

8. Configure Environment Variables

Set up environment variables for your Node.js applications to manage configurations efficiently:

            
nano ~/.bashrc # Add the following lines: export NODE_ENV=production export PORT=8000 # Save and exit, then run: source ~/.bashrc

9. Use NVM (Node Version Manager)

NVM allows you to install and manage multiple versions of Node.js. To install NVM:

            
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash # Restart your terminal, then install Node.js: nvm install 18 nvm use 18

10. Check Installed Versions with NVM

            
nvm ls nvm current

11. Set Default Node Version

            
nvm alias default 18

12. Install Global Packages

You can install global packages that you frequently use:

            
npm install -g express npm install -g nodemon

13. Monitor Node.js Processes

Use PM2 to manage and monitor your Node.js applications:

            
sudo npm install -g pm2 pm2 start app.js pm2 list