Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Deploy a Node.js App with npm

This guide shows you how to put a Node.js app on a server, install its packages with npm and keep it running. It fits a VPS or dedicated server where you have root access.

How-To Guides2 min read8 steps

What are Node.js and npm?

Node.js lets you run JavaScript on a server instead of only in a browser. npm is the tool that downloads packages for Node.js. A package is a bundle of ready-made code. To deploy means to move your app to a server and start it so everyone can use it.

Some shared hosting plans have a Node.js tool in the control panel. Ask Hostvento support whether your plan has one.

Steps

  1. Log in to your server with SSH. SSH is a safe way to type commands on a server from far away.
  2. Check that Node.js and npm are installed.
    node -v
    npm -v
    If you see "command not found", install them with your system's package tool.
  3. Copy your app to the server. You can use Git or an upload tool like SFTP.
    git clone https://example.com/your/repo.git myapp
    cd myapp
    Use your own repository address.
  4. Install the packages your app needs.
    npm install --omit=dev
    This reads package.json and downloads everything in it. The option skips tools used only for development.
  5. Test the app.
    npm start
    This runs the start script from package.json. Stop it with Ctrl+C after you see it works.
  6. Install PM2. PM2 is a tool that keeps your app running and restarts it if it crashes.
    sudo npm install -g pm2
  7. Start your app with PM2.
    pm2 start npm --name myapp -- start
  8. Make it start again after a reboot.
    pm2 save
    pm2 startup
    Run the command that pm2 startup prints.

Make it reachable

Your app listens on a port, such as 3000. A port is like a numbered door. Visitors expect the normal web doors, 80 and 443. Set up a reverse proxy with Nginx or Apache to pass traffic from those doors to your app. Also open the right ports in your firewall.

Keep it safe

  • Keep secrets in an .env file. Never upload that file to a public repository.
  • Take a backup before you update.
  • Run pm2 logs myapp to read errors.
Tip: To update later, pull your new code, run npm install, then pm2 restart myapp.

Quick recap

  • Copy your app, run npm install, test with npm start.
  • Use PM2 to keep it running.
  • Use a reverse proxy to serve it on ports 80 and 443.