7 Topics
Chapter•35 Minutes

Environment and Project Setup

Install Node.js, initialize the project, configure dependencies, and create the initial Express application.

Install Node.js and npm

Socket.IO runs on Node.js, so you need to install Node.js before creating the application.

Node.js includes npm, which is the package manager used to install project dependencies.

  1. Download and install a supported Node.js LTS release.
  2. Open your terminal or command prompt.
  3. Verify the installation.

The following commands display the installed versions.

Requirements

Runtime

Node.js 20+ recommended

Package Manager

npm

Editor

Visual Studio Code

Browser

Chrome, Firefox, or Edge

Knowledge

Basic JavaScript

Node.js Version

Use an active LTS version of Node.js. Check the requirements of your chosen Socket.IO and other dependency versions before deploying.

Verify Node.js and npm
node --version
npm --version

The first command displays the installed Node.js version. The second command displays the npm version.

Key Takeaways
  • Node.js is required to run the backend.

  • npm manages project dependencies.

  • Verify both installations before proceeding.

Create the Node.js Project

Create a dedicated folder for the chat application.

Open your terminal and execute the following commands:

  1. Create the project directory.
  2. Navigate into the directory.
  3. Initialize npm.

The npm init -y command generates a default package.json file.

The package.json file stores project metadata, dependencies, scripts, and module configuration.

Initialize the Project
mkdir socket-io-chat
cd socket-io-chat
npm init -y

These commands create the project directory and generate the package.json configuration file.

Key Takeaways
  • Create a dedicated directory for the application.

  • Initialize the project with npm.

  • package.json manages dependencies and scripts.

Configure ES Modules

We will use JavaScript ES Modules throughout this tutorial.

ES Modules allow you to use import and export syntax instead of CommonJS require() and module.exports.

Open package.json and add the type property.

The project configuration should look like this:

Module System

This tutorial consistently uses ES Modules. Do not mix require() and import syntax in the same project unless you intentionally configure interoperability.

package.json
{
  "name": "socket-io-chat",
  "version": "1.0.0",
  "description": "Real-time chat application using Node.js and Socket.IO",
  "type": "module",
  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch src/server.js"
  }
}

The type property tells Node.js to interpret .js files as ES Modules. The start script runs the application, while the dev script restarts the application when watched files change.

Key Takeaways
  • The type module setting enables ES Modules.

  • npm scripts simplify application execution.

  • Node.js watch mode is useful during development.

Install Express and Socket.IO

Now install the packages required by the application.

We need two main dependencies:

  • express: Creates the HTTP server application and handles HTTP routes.
  • socket.io: Provides real-time event communication.

We will also use dotenv to load environment variables and cors only if we later separate the frontend and backend origins.

For this initial implementation, the frontend will be served by Express from the same origin, so separate CORS configuration is unnecessary.

Install Dependencies

npm install express socket.io dotenv

Installs Express, the Socket.IO server package, and dotenv for environment configuration.

Key Takeaways
  • Express handles HTTP requests.

  • Socket.IO handles real-time communication.

  • dotenv loads environment configuration.

Create the Project Directory Structure

Before writing application code, organize the project into separate files.

Create the following structure:

socket-io-chat/
│
├── public/
│   ├── index.html
│   ├── style.css
│   └── app.js
│
├── src/
│   ├── config/
│   │   └── socket.js
│   ├── events/
│   │   └── chat.events.js
│   └── server.js
│
├── .env
├── .gitignore
└── package.json

Responsibilities:

FileResponsibility
server.jsCreates and starts the HTTP server
socket.jsInitializes Socket.IO
chat.events.jsRegisters chat-related event handlers
index.htmlDefines the chat interface
style.cssStyles the chat interface
app.jsConnects the browser to Socket.IO
.envStores environment variables
.gitignoreExcludes sensitive and generated files

This separation makes the project easier to understand and maintain as additional features are introduced.

Create Directories
mkdir -p public src/config src/events

Creates the public folder and the backend configuration and event-handler directories. On Windows PowerShell, create the folders using New-Item -ItemType Directory -Force public, src/config, src/events.

Key Takeaways
  • Keep frontend and backend code organized.

  • Separate Socket.IO configuration from event handlers.

  • A clear directory structure simplifies future development.

Configure Environment Variables

Environment variables allow you to configure application settings without hardcoding them into source files.

Create a .env file in the project root.

Add the following configuration:

Environment Variables

PORT

3000

NODE_ENV

development

Protect Environment Files

Never commit .env files containing secrets or credentials to a public repository. Add .env to .gitignore.

.env
PORT=3000
NODE_ENV=development

PORT defines the HTTP server port. NODE_ENV identifies the current application environment.

Key Takeaways
  • Environment variables separate configuration from source code.

  • The server port can be changed without modifying application code.

  • Sensitive configuration must not be committed to Git.

Configure .gitignore

Create a .gitignore file in the project root.

Add the following entries:

This prevents node_modules, local environment files, and debug logs from being included in Git commits.

.gitignore
node_modules/
.env
.env.*
!.env.example
npm-debug.log*
.DS_Store

These patterns exclude installed dependencies, environment files, and common local development artifacts from version control.

Key Takeaways
  • Do not commit node_modules.

  • Keep local environment files out of Git.

  • Maintain an environment example file when documenting required configuration.