Getting Started with Node.js and npm: Your First Steps into JavaScript Development
A step-by-step guide for Python developers transitioning to Node.js and npm. Learn installation, project setup, package management, and building a simple web server.
Advertisement
If you've been working with Python and want to explore JavaScript on the server side, Node.js is where you'll want to start. I remember when I first made the switch from Python to Node.js at PythonSkillset — it felt like learning a new dialect of a language I already knew. The concepts were familiar, but the tools were different. Let me walk you through setting up Node.js and npm, the package manager that comes with it.
What Exactly Are Node.js and npm?
Node.js is a runtime environment that lets you run JavaScript outside of a web browser. Think of it like Python's interpreter, but for JavaScript. It's built on Chrome's V8 engine, which means it's fast and efficient.
npm (Node Package Manager) is exactly what it sounds like — a tool for managing JavaScript packages. If you've used pip in Python, you'll feel right at home. npm handles installing, updating, and removing libraries for your projects.
Installing Node.js and npm
The easiest way to get started is by downloading the installer from the official Node.js website. You'll see two versions: LTS (Long Term Support) and Current. For beginners, I always recommend the LTS version — it's stable and well-tested.
On Windows or macOS: 1. Go to nodejs.org 2. Download the LTS installer for your operating system 3. Run the installer — it will set up both Node.js and npm automatically 4. Follow the installation wizard, accepting the default settings
On Linux (Ubuntu/Debian):
sudo apt update
sudo apt install nodejs npm
After installation, verify everything worked by opening your terminal and running:
node --version
npm --version
You should see version numbers for both. If you do, congratulations — you're ready to start building.
Your First Node.js Project
Let's create something simple to make sure everything is working. Create a new folder for your project and navigate into it:
mkdir my-first-node-project
cd my-first-node-project
Now, initialize a new Node.js project:
npm init -y
This creates a package.json file — think of it as the blueprint for your project. It stores metadata about your project and lists all the dependencies (packages your project needs to run).
Installing Your First Package
Let's install a useful package called chalk that adds color to your terminal output:
npm install chalk
You'll notice a few things happened:
- A node_modules folder appeared — this is where all installed packages live
- A package-lock.json file was created — it locks down exact versions of dependencies
- The package.json file now lists chalk as a dependency
Writing Your First Node.js Script
Create a file called app.js and add this code:
const chalk = require('chalk');
console.log(chalk.green('Hello from Node.js!'));
console.log(chalk.blue('This text is blue'));
console.log(chalk.red.bold('This is bold red text'));
Run it with:
node app.js
You should see colorful text in your terminal. Simple, but it proves everything is working.
Understanding package.json
The package.json file is the heart of any Node.js project. Here's what a basic one looks like:
{
"name": "my-first-node-project",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The dependencies section will automatically update when you install packages. This file is crucial because it lets anyone else (or your future self) know exactly what packages your project needs.
Working with npm Commands
Here are the essential npm commands you'll use daily:
Installing packages:
- npm install <package-name> — installs a package and adds it to dependencies
- npm install -g <package-name> — installs globally (use sparingly)
- npm install --save-dev <package-name> — installs as a development dependency
Managing packages:
- npm uninstall <package-name> — removes a package
- npm update — updates all packages to their latest versions
- npm list — shows all installed packages
Creating a Simple Web Server
Let's build something practical — a basic web server. Create a new file called server.js:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from PythonSkillset Node.js tutorial!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Run it with node server.js, then open your browser to http://localhost:3000. You'll see your message displayed. Press Ctrl+C in the terminal to stop the server.
Understanding the node_modules Folder
When you install packages, npm downloads them into a node_modules folder. This folder can get quite large — a typical project might have hundreds of packages in there. Don't worry, this is normal. The important thing is to never manually edit files in node_modules. Always use npm commands to manage packages.
The package-lock.json File
You'll notice a package-lock.json file appeared alongside package.json. This file locks down the exact version of every package and its dependencies. It ensures that when someone else clones your project and runs npm install, they get exactly the same versions you're using. This prevents the dreaded "it works on my machine" problem.
Creating a Simple Web Server
Let's build something more practical — a web server that serves an HTML page. Create a new file called server.js:
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.writeHead(500);
res.end('Server error');
return;
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
} else {
res.writeHead(404);
res.end('Page not found');
}
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Create an index.html file in the same folder:
<!DOCTYPE html>
<html>
<head>
<title>My First Node.js App</title>
</head>
<body>
<h1>Welcome to PythonSkillset!</h1>
<p>This page is being served by Node.js.</p>
</body>
</html>
Run node server.js and visit http://localhost:3000 — you'll see your HTML page being served.
Understanding the node_modules Folder
When you install packages, npm downloads them into a node_modules folder. This folder can get huge — a typical project might have hundreds of packages in there. Don't panic, this is normal. The important thing is to never manually edit files in node_modules. Always use npm commands to manage packages.
The package-lock.json File
You'll notice a package-lock.json file appeared alongside package.json. This file locks down the exact version of every package and its dependencies. It ensures that when someone else clones your project and runs npm install, they get exactly the same versions you're using. This prevents the dreaded "it works on my machine" problem.
Common npm Commands You'll Use Daily
Here's a quick reference for the commands you'll use most often:
npm init— creates a new package.json filenpm install <package>— installs a package and saves it to dependenciesnpm install --save-dev <package>— installs as a development dependencynpm uninstall <package>— removes a packagenpm update— updates all packagesnpm list— shows installed packages
A Real-World Example: Building a Simple API
Let's build something more practical — a simple API that returns JSON data. Create a new file called api.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
if (req.url === '/api/users') {
const users = [
{ id: 1, name: 'Alice', role: 'Developer' },
{ id: 2, name: 'Bob', role: 'Designer' },
{ id: 3, name: 'Charlie', role: 'Manager' }
];
res.end(JSON.stringify(users));
} else {
res.end(JSON.stringify({ message: 'Welcome to the API' }));
}
});
server.listen(3000, () => {
console.log('API server running at http://localhost:3000/');
});
Run it and visit http://localhost:3000/api/users — you'll get a JSON response with user data. This is the foundation of building APIs with Node.js.
Common Pitfalls and How to Avoid Them
1. Forgetting to add packages to package.json
Always use npm install <package> --save (or just npm install <package> in modern npm) to ensure the package is recorded in your dependencies.
2. Ignoring the node_modules folder in version control
Add node_modules to your .gitignore file. Never commit it to version control — it's huge and can be regenerated with npm install.
3. Using global installs unnecessarily Only install packages globally if you need them as command-line tools. For project dependencies, always install locally.
Next Steps
Now that you have Node.js and npm set up, you can start building real applications. Try creating a simple REST API, a command-line tool, or even a basic web server. The JavaScript ecosystem is vast, and npm has over 2 million packages available.
At PythonSkillset, we've found that the transition from Python to Node.js is smoother than most people expect. The concepts of modules, packages, and dependency management translate directly. The main difference is the syntax and the asynchronous nature of JavaScript.
Common Issues and Fixes
"Command not found" after installation Restart your terminal or command prompt. If that doesn't work, check that Node.js is in your system PATH.
Permission errors on Linux/Mac
When installing global packages, you might need to use sudo on Linux/Mac. A better approach is to configure npm to install global packages in your user directory:
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm install taking forever This usually happens with slow internet connections. Try using a different registry mirror:
npm config set registry https://registry.npmmirror.com
What's Next?
Now that you have Node.js and npm set up, you can start building real applications. Try creating a simple REST API, a command-line tool, or even a basic web server. The JavaScript ecosystem is vast, and npm has over 2 million packages available.
At PythonSkillset, we've found that the transition from Python to Node.js opens up new possibilities, especially for real-time applications and microservices. The asynchronous nature of Node.js makes it particularly good for handling many concurrent connections.
Remember, the key to learning Node.js is to start small and build up. Don't try to learn everything at once. Just get comfortable with the basics — installing packages, creating modules, and running scripts — and you'll be building full applications before you know it.
Advertisement
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.