What you're actually doing when you deploy Node.js

Deploying Node.js means taking code you've written on your computer and putting it on a server that runs 24/7, so other people can use it. Your local machine shuts down; the server doesn't. You're moving from "this works when I run it" to "this works when anyone visits it from anywhere."

The process has three real parts: creating a Node.js project on your machine, preparing it so a server can run it, and then moving it to a hosting provider that will keep it running. Most of the confusion comes from the middle step — there are many ways to prepare code, and different hosting providers expect different things.

This guide walks you through creating a basic Node.js application, testing it locally, and deploying it to a free or low-cost hosting service. You'll use tools that are standard in the industry, so what you learn here transfers to other projects and other hosts.

Key Takeaways

  • Node.js is a runtime that lets you write server code in JavaScript; you download it from nodejs.org and install it on your machine before you can create anything.
  • A Node.js project starts with a folder, a package.json file that lists what your code needs, and an index.js or server.js file where your code actually lives.
  • You test locally by running your code on your own computer first, using the command line to start the server and a browser to visit it at localhost:3000 or similar.
  • Deployment means moving your code and its dependencies to a hosting provider; popular free or cheap options include Render, Railway, and Heroku, each with slightly different setup steps.
  • Your hosting provider needs a way to start your application automatically, which you specify in package.json using a "start" script.

Install Node.js and create your first project

Go to nodejs.org and download the LTS (Long Term Support) version for your operating system — Windows, Mac, or Linux. Run the installer and follow the prompts. When it finishes, open your command line (Terminal on Mac, Command Prompt or PowerShell on Windows) and type node --version. You should see a version number like v18.17.0 or higher. If you see an error, restart your command line and try again.

Create a new folder for your project. You can do this in your file explorer or from the command line. Open your command line, navigate to where you want the folder to live, and type mkdir my-node-app (replace "my-node-app" with whatever you want to call it). Then type cd my-node-app to move into that folder.

Now initialize a Node.js project by typing npm init -y. This creates a file called package.json, which is a list of instructions for Node.js about what your project needs and how to run it. The -y flag says "use all the defaults" so you don't have to answer questions. You can edit this file later if you need to.

Create a file called index.js in the same folder. This is where your actual code will live. You can create it in any text editor — VS Code is popular and free, but Notepad works too.

Write a basic server that responds to requests

Open your index.js file and paste this code:

const http = require('http'); const port = process.env.PORT || 3000; const server = http.createServer((req, res) => {   res.statusCode = 200;   res.setHeader('Content-Type', 'text/plain');   res.end('Hello World\n'); }); server.listen(port, () => {   console.log(`Server running at http://localhost:${port}/`); });

Save the file. This code creates a web server that listens for requests on port 3000 (or whatever port your hosting provider assigns). When someone visits it, the server responds with "Hello World". The line process.env.PORT || 3000 means "use the PORT environment variable if it exists, otherwise use 3000" — this is important for deployment, because your hosting provider will set PORT automatically.

Open your command line, make sure you're in your project folder (type pwd on Mac or cd on Windows to check), and type node index.js. You should see "Server running at http://localhost:3000/". Open a web browser and visit http://localhost:3000. You should see "Hello World" on the page. This means your server is working.

To stop the server, go back to your command line and press Ctrl+C (or Cmd+C on Mac). The server will shut down.

Update package.json so your host knows how to start the server

Open your package.json file. You'll see something like this:

{   "name": "my-node-app",   "version": "1.0.0",   "description": "",   "main": "index.js",   "scripts": {     "test": "echo \"Error: no test specified\" && exit 1"   },   "keywords": [],   "author": "",   "license": "ISC" }

Find the "scripts" section. Replace the "test" line with "start": "node index.js". Your scripts section should now look like this:

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

Save the file. This tells your hosting provider that when it needs to start your application, it should run the command node index.js. Without this, the host won't know what to do.

Test this locally by typing npm start in your command line instead of node index.js. You should see the same "Server running" message. Press Ctrl+C to stop it.

Push your code to GitHub so your host can access it

Most hosting providers pull your code from GitHub rather than uploading files manually. Go to github.com and create a free account if you don't have one. Click the "+" icon in the top right and select "New repository". Name it the same as your project folder, leave it public, and click "Create repository".

GitHub will show you commands to run. Open your command line in your project folder and run these commands in order. They look something like:

git init git add . git commit -m "Initial commit" git branch -M main git remote add origin https://github.com/YOUR-USERNAME/my-node-app.git git push -u origin main

Replace YOUR-USERNAME with your actual GitHub username and my-node-app with your repository name. If you get an error about git not being installed, download it from git-scm.com and install it first.

When the push finishes, go back to your GitHub repository page and refresh. You should see your index.js and package.json files listed there. Your code is now on GitHub and ready to deploy.

Deploy to a hosting provider using GitHub

Render is one of the easiest free options. Go to render.com, click "Sign up", and create an account using your GitHub login. Click "New +" and select "Web Service". Select "Connect a repository" and choose the repository you just created. Give your service a name (it can be anything), make sure the "Start Command" field shows npm start, and click "Create Web Service".

Render will take 2 to 5 minutes to build and deploy your code. You'll see a log of what's happening on the screen. When it finishes, you'll see a URL at the top like https://my-node-app-abc123.onrender.com. Click that URL and you should see "Hello World" displayed. Your application is now live on the internet.

If you see an error instead, scroll down in the Render dashboard and look at the build log. Common errors include a missing "start" script in package.json (which you already fixed) or a typo in your code. Fix the error, commit and push the change to GitHub with git add ., git commit -m "Fix error", and git push. Render will automatically redeploy.

Other hosting options include Railway (railway.app) and Heroku (heroku.com), which work similarly — connect your GitHub repository, specify the start command, and they deploy automatically. Heroku's free tier ended in 2022, but Railway and Render both offer free plans that work for learning and small projects.

Make changes and redeploy without manual work

One of the benefits of connecting GitHub to your host is that redeployment happens automatically. Change your code locally, test it with npm start, then commit and push to GitHub:

git add . git commit -m "Describe what you changed" git push

Within a few seconds to a few minutes, your hosting provider will see the change, rebuild your application, and deploy the new version. You don't have to do anything else. Visit your live URL and you'll see the updated code.

This workflow — write locally, test locally, push to GitHub, auto-deploy — is how professional teams work. You're learning the real process, not a shortcut.

Frequently Asked Questions

What's the difference between localhost:3000 and my live URL?

Localhost is your own computer. Only you can visit localhost:3000 because it's running on your machine. Your live URL is on a server that's always on, so anyone on the internet can visit it. The code is the same; the location is different.

Do I need to pay for hosting?

No. Render, Railway, and other providers offer free tiers that work for learning and small projects. They may put your app to sleep if it's not used for a while, which means the first request takes longer to respond, but it's genuinely free. If you need may provide uptime or more power, paid plans start around $5 to $10 per month.

What happens if my code has a bug after I deploy?

Your hosting provider will show you a build log or error message. Fix the bug in your local code, test it with npm start, then push to GitHub. The host will redeploy automatically. Your live site will be broken only until you push the fix, which usually takes a few minutes.

Can I use a database with Node.js?

Yes. You can connect to databases like MongoDB, PostgreSQL, or MySQL the same way you connect to anything else — by installing a package with npm and writing code to use it. The database itself runs on a separate server, and your Node.js code talks to it. Most hosting providers support this setup.

What if I want a custom domain instead of the render.com URL?

Most hosting providers let you connect a custom domain for a small fee. You buy the domain from a registrar like Namecheap or GoDaddy, then point it to your hosting provider's servers using DNS settings. Your host's documentation explains the exact steps for your provider.