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.
- Download and install a supported Node.js LTS release.
- Open your terminal or command prompt.
- Verify the installation.
The following commands display the installed versions.
Requirements
Node.js 20+ recommended
npm
Visual Studio Code
Chrome, Firefox, or Edge
Basic JavaScript
Use an active LTS version of Node.js. Check the requirements of your chosen Socket.IO and other dependency versions before deploying.
node --version
npm --versionThe first command displays the installed Node.js version. The second command displays the npm version.
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:
- Create the project directory.
- Navigate into the directory.
- 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.
mkdir socket-io-chat
cd socket-io-chat
npm init -yThese commands create the project directory and generate the package.json configuration file.
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:
This tutorial consistently uses ES Modules. Do not mix require() and import syntax in the same project unless you intentionally configure interoperability.
{
"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.
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 dotenvInstalls Express, the Socket.IO server package, and dotenv for environment configuration.
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:
| File | Responsibility |
|---|---|
| server.js | Creates and starts the HTTP server |
| socket.js | Initializes Socket.IO |
| chat.events.js | Registers chat-related event handlers |
| index.html | Defines the chat interface |
| style.css | Styles the chat interface |
| app.js | Connects the browser to Socket.IO |
| .env | Stores environment variables |
| .gitignore | Excludes sensitive and generated files |
This separation makes the project easier to understand and maintain as additional features are introduced.
mkdir -p public src/config src/eventsCreates 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.
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
3000
development
Never commit .env files containing secrets or credentials to a public repository. Add .env to .gitignore.
PORT=3000
NODE_ENV=developmentPORT defines the HTTP server port. NODE_ENV identifies the current application environment.
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.
node_modules/
.env
.env.*
!.env.example
npm-debug.log*
.DS_StoreThese patterns exclude installed dependencies, environment files, and common local development artifacts from version control.
Do not commit node_modules.
Keep local environment files out of Git.
Maintain an environment example file when documenting required configuration.