Setting Up a Node.js Project
Initialize a new Node.js project and install the necessary WebSocket library (e.g., 'ws').
Setting Up a Node.js Project is a free WebSockets & Realtime Systems Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Realtime Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome: Setting Up Node.js
Get ready to build your first WebSocket server! Our journey begins by setting up a new Node.js project. Node.js is a powerful JavaScript runtime that lets you run JavaScript code outside of a web browser.
This lesson will guide you through initializing your project and installing essential libraries.
Verify Node.js Installation
First things first, let's ensure Node.js and its package manager, npm (Node Package Manager), are installed on your system. Open your terminal or command prompt and run these commands:
node -v
npm -vInitialize Your Project Folder
Every Node.js project typically lives in its own folder. Let's create one and then use npm init to set up the project. The -y flag answers all prompts with default values.
mkdir my-websocket-server
cd my-websocket-server
npm init -yUnderstanding package.json
After running npm init -y, you'll find a new file named package.json in your project folder. This file is crucial! It acts as a manifest for your project, containing metadata and a list of all external libraries (dependencies) your project relies on.
{
"name": "my-websocket-server",
"version": "1.0.0",
"description": "My first Node.js WebSocket server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}Installing the 'ws' Library
To build a WebSocket server, we need a special library to handle the WebSocket communication protocol. The ws library is a very popular, fast, and simple choice for Node.js.
Let's install it using npm:
npm install wsExploring node_modules
Once ws is installed, you'll see a new folder named node_modules appear in your project directory. This is where npm stores all the packages your project depends on, including ws and any of its own dependencies.
It can get quite large! You generally don't modify files inside node_modules directly.
Your First Node.js Script
Let's create a simple JavaScript file, index.js, to confirm our Node.js setup is working. In your project folder, create index.js and add this code:
// index.js
console.log("Hello CoddyKit from Node.js!");Running Your Node.js Script
Now that you have your index.js file, you can run it directly using the node command in your terminal. Make sure you are in your project's root directory.
node index.jsDependencies in package.json
After installing ws, if you open your package.json again, you'll notice a new "dependencies" section. This lists all the external packages your project needs to function, ensuring others can easily set up your project.
{
"name": "my-websocket-server",
"version": "1.0.0",
"description": "My first Node.js WebSocket server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ws": "^8.18.0"
}
}Quick Check: Project Setup
You've learned the basics of setting up a Node.js project. Let's test your knowledge!
Recap & Next Steps
Fantastic work! You've successfully set up your first Node.js project for building a WebSocket server. Here's what we covered:
- Verified Node.js and npm installations.
- Initialized a new project with
npm init. - Understood the role of
package.jsonandnode_modules. - Installed the
wslibrary. - Ran a basic Node.js script.
Next, we'll dive into writing the actual server-side logic to handle WebSocket connections!
Frequently asked questions
Is the “Setting Up a Node.js Project” lesson free?
Yes — the full text of “Setting Up a Node.js Project” is free to read here on the web, and the WebSockets & Realtime Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Realtime Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Setting Up a Node.js Project”?
Initialize a new Node.js project and install the necessary WebSocket library (e.g., 'ws'). You practise WebSockets & Realtime Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Realtime Systems Programming?
No prior experience is required. WebSockets & Realtime Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Setting Up a Node.js Project” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Realtime Systems Programming lesson?
Yes. Every WebSockets & Realtime Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up a Node.js Project
- Implementing Server-Side Logic
- Broadcasting Messages to Clients
- Managing Rooms and Channels