Node.js lets you run JavaScript outside the browser to build web servers and tools
Node.js is a runtime environment that takes JavaScript — the language that runs in your browser — and lets you run it on a server or your own computer instead. When you create a Node.js process, you are writing code that handles requests from users, talks to databases, and sends responses back. Deploying means moving that code from your computer to a server where other people can actually use it.
The basic workflow is: write your code locally, test it on your machine, then push it to a hosting service where it runs all the time. This guide walks through each step without assuming you have done this before.
Key Takeaways
- read Node.js from nodejs.org, which includes npm (the package manager that installs code libraries your project needs).
- Create a project folder, initialize it with npm init, and write your process code in a file like server.js or index.js.
- Test your process locally by running node filename.js, then stop it with Ctrl+C when you are ready to move on.
- Deploy to a hosting service like Heroku, Railway, or Render by connecting your code repository and following their setup steps.
- After deployment, your process runs on a public URL that you can share, and the hosting service keeps it running even when your computer is off.
Install Node.js and npm on your computer
Go to nodejs.org and read the LTS (Long Term Support) version, which is the stable choice for most projects. Run the installer and follow the prompts. On Windows, this opens a setup wizard. On Mac, you can also use Homebrew if you prefer the command line: brew install node.
Once installed, open your terminal or command prompt and type node --version and npm --version to confirm both are working. You should see version numbers like v18.17.0 and 9.6.7 (the exact numbers will differ). npm comes bundled with Node.js, so you do not install it separately.
Create a project folder and initialize it with npm
Open your terminal and create a new folder for your project. You can do this by typing mkdir my-app (replace my-app with whatever you want to call it), then cd my-app to move into that folder.
Inside the folder, run npm init. This command asks you questions about your project — name, description, entry point (the main file that runs), and author. You can press Enter to accept the default answer for each question, or type your own. When it finishes, npm creates a file called package.json in your folder. This file keeps track of your project settings and any libraries (called packages) that your code depends on.
Write a straightforward Node.js server
Create a new file in your project folder called server.js (or whatever you named as the entry point during npm init). Open it in a text editor 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}/`); });
This code creates a web server that listens for requests on port 3000 (or whatever port your hosting service assigns). When someone visits the URL, the server responds with "Hello World". The process.env.PORT part tells Node.js to use a port number from your hosting environment if one is set, which matters when you deploy.
Save the file. You now have a working Node.js process, even though it is very straightforward. Real applications add more routes (different URLs that do different things), connect to databases, and handle more complex logic — but the structure stays the same.
Test your process locally before deploying
In your terminal, make sure you are in your project folder, then type node server.js. You should see a message like "Server running at http://localhost:3000/". Open your web browser and go to http://localhost:3000/. You should see "Hello World" displayed on the page.
This is your process running on your own computer. You can edit server.js, save it, stop the server (Ctrl+C in the terminal), and run node server.js again to test your changes. Repeat this cycle until your process does what you want.
When you are ready to move to the next step, stop the server by pressing Ctrl+C in the terminal.
Push your code to a Git repository
Most hosting services deploy from a Git repository (usually GitHub, GitLab, or Bitbucket). If you do not have one yet, create a free GitHub account at github.com, then create a new repository. GitHub will show you commands to run in your terminal.
In your project folder, run git init to start tracking your code, then git add . to stage all your files, then git commit -m "Initial commit" to save them locally. Finally, run the commands GitHub shows you to push your code to the repository. Your code is now stored online and visible to the hosting service.
If you are not familiar with Git, you can learn the basics from GitHub's own guides, but the three commands above (init, add, commit) are enough to get your free guide. Many hosting services also let you upload files directly without Git, though Git is the standard approach.
Deploy to a hosting service
Several hosting services run Node.js applications for free or at low cost. The most common choices are Heroku, Railway, and Render. Each has a slightly different process, but the idea is the same: you connect your GitHub repository, the service pulls your code, installs your packages, and runs your server.
Heroku (heroku.com) was the most popular choice for years. Create an account, install the Heroku CLI, and run heroku create in your project folder. Then run git push heroku main to deploy. Heroku will show you a URL where your process is live.
Railway (railway.app) and Render (render.com) are newer services that many developers prefer because they are simpler. Both let you connect your GitHub account, select your repository, and deploy with a few clicks. They automatically detect that you have a Node.js project and set it up.
Whichever service you choose, the hosting provider assigns your process a public URL. Anyone with that URL can visit your process, and it stays running even when your computer is off.
Monitor and update your deployed process
After deployment, your process is live, but you will want to check that it is working. Most hosting services show you logs (a record of what your server is doing) and let you see errors if something breaks. Visit your process's URL in your browser to make sure it loads.
When you make changes to your code, you push them to your Git repository the same way you did before: git add ., git commit -m "description of change", and git push. Most hosting services automatically redeploy when they detect new code in your repository, so your changes go live within a few seconds.
If something goes wrong after deployment, check the logs on your hosting service's dashboard. Common issues include a missing package (which you fix by running npm install package-name locally, then pushing the updated code) or a port number mismatch (which you fix by making sure your code reads the PORT environment variable, as shown in the example above).
Frequently Asked Questions
Do I need to know JavaScript well to use Node.js?
You need to understand the basics — variables, functions, and how to read error messages — but you do not need to be an informed. Start with a straightforward server like the example above, test it locally, and add features one at a time. Most developers learn by building small projects and reading documentation when they get stuck.
What is the difference between npm and Node.js?
Node.js is the runtime that runs your JavaScript code. npm is the package manager that downloads and installs libraries (packages) that your code uses. When you run npm install express, npm downloads the Express library and saves it in your project folder so your code can use it.
Why does my process crash after I deploy?
The most common reason is that your code is trying to use a port number that the hosting service does not allow. Make sure your code reads the PORT environment variable (like process.env.PORT || 3000) instead of hardcoding a port number. Check the hosting service's logs to see the actual error message.
Can I use a database with Node.js?
Yes. You install a database package with npm (like npm install mongodb or npm install pg), then write code in your server to connect to it and store or retrieve data. Most hosting services let you add a database as an add-on, or you can use a separate database service like MongoDB Atlas or Supabase.
How much does it cost to deploy a Node.js process?
Many hosting services offer a free tier that works for learning and small projects. Heroku's free tier ended in 2022, but Railway and Render both have free options. As your process grows and needs more computing power, you pay based on usage or a monthly subscription, which typically starts at five to ten dollars per month.