How to host a Node.js app for free (multiple ways)

Updated: December 30, 2023 By: Guest Contributor Post a comment

Introduction

As a Node.js developer, you’ve likely spent lots of time perfecting your application. However, after all those hours of development, you want to showcase your work to the world. Luckily, hosting a Node.js application doesn’t need to be expensive; there are services out there that provide free hosting solutions — ideal for personal projects, MVPs, or proof-of-concepts. In this tutorial, we will walk through several platforms, guiding you from the basics of deployment to more advanced automation techniques, all without reaching for your wallet.

Hosting on Heroku

Heroku is a popular cloud service that offers a free tier for small-scale projects. Get started by installing the Heroku CLI and creating a git repository if you haven’t done so already.

npm install -g heroku
heroku login

Create a Procfile without a file extension in the root of your project directory and specify the command to run your application:

web: node index.js

Before deploying, make sure your package.json has the start script:

{
  "scripts": {
    "start": "node index.js"
  }
}

Deploy your application to Heroku:

git init
heroku create
git add .
git commit -m "Initial commit"
git push heroku master

Now, your application should be live. To troubleshoot, use heroku logs --tail.

Hosting on Vercel

Vercel, the platform behind Next.js, also offers free hosting for Node.js applications with a few simple steps.

Firstly, push your code to a Git repository and make sure your entry file is named index.js or update your package.json accordingly:

{
  "scripts": {
    "start": "node server.js"
  }
}

Sign up to Vercel and select your repository. If your setup is standard, Vercel should pick up your settings naturally. You can customize the command and publish directory, but with defaults, deploy at a button-click — all straight from your GitHub repo.

Vercel’s deploy notifications, preview URLs for branch commits, and seamless Git integration make it incredibly friendly for iterative development.

Hosting on Glitch

Glitch is a community-driven platform that offers real-time collaborative project editing, and yes, they provide free hosting.

The great thing about using Glitch is that it has a built-in code editor and terminal, so you can write and import your Node.js code directly.

Once you have your code ready, compiling and wakes up automatically can be deployed. You’ll get an instant live URL, and the service keeps your project live as long as you remain regularly active on it.

GitHub Actions for Continous Deployment

For a more advanced approach, GitHub Actions can help you automate your deployment process.

Assuming you’ve decided to host your Node.js application on these mentioned platforms using continuous deployment, create a tiket GitHub workflow in the .github/workflows directory of your project.

A basic workflow to deploy to Heroku after any push to the main branch:

name: Deploy to Heroku

on:
  push:
    branches: [main]

jobs:
  deploy:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Login to Heroku
      uses: akhileshns/[email protected]
      with:
        heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
        heroku_app_name: YOUR_APP_NAME
        heroku_email: YOUR_EMAIL

Remember to replace YOUR_APP_NAME, YOUR_EMAIL, and to add HEROKU_API_KEY to your GitHub secrets found in repository settings.

Using Docker with a Cloud Provider

If you have Docker knowledge, building your Node.js app as a Docker image can be a welcomed scalability advancement. Many cloud services allow hosting Docker containers on a free tier such as Google Cloud Run or Oracle Cloud.

First, write your Dockerfile:

FROM node:latest
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["node", "index.js"]
EXPOSE 8080

Then, use Docker commands to build and run your image locally:

docker build -t nodeapp .
docker run -p 8080:8080 nodeapp

Push it to a container registry and deploy using the preferred cloud service instructions, configuring services like authentication and networking as needed.

Conclusion

In this tutorial, we’ve learned that hosting a Node.js application can indeed be free and suitable for a variety of use cases, from prototyping to personal projects. We discussed deploying to Heroku, using Vercel, exploiting Glitch for quick hosting, the more automated approach using GitHub Actions, and even how to make use of containers with Docker for deployment. Allowing your creativity to flow shouldn’t be limited by hosting costs; these platforms empower developers with different skillsets to share applications with the world effortlessly.

Many roads lead to a successful deployment, each with features unique to its architect, yet every one of them holding a unanimous essence—the shared mission of democratizing application hosting for developers everywhere.