NODE: Getting Started with Node.js

A practical guide to initializing a Node.js project, managing dependencies with npm, and running your first server using Express.js. Learn how package.json, node_modules, and npm scripts form the foundation of every Node.js application.

Once Node.js is installed, the fastest way to understand it is to build something with it. This guide walks through the practical steps of starting a project, managing packages, and running a working web server from scratch.


Step 1: Initialize a Project

Every Node.js project starts with a package.json file. This file stores metadata about your project — its name, version, description, entry point, and dependencies.

To create it automatically, run:

bash
npm init -y

The -y flag skips the interactive prompt and accepts all defaults. The generated package.json looks like this:

json
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

You can edit this file manually at any time to update the project name, description, or scripts.


Step 2: Install Packages

Node.js projects rely on npm packages — reusable libraries published to the npm registry. You install them into a node_modules directory inside your project.

Installing a Production Dependency

To install Express.js, a widely used web framework:

bash
npm install express

This command:

  • Downloads Express and its dependencies into node_modules/
  • Adds Express to the dependencies section of package.json
  • Creates or updates package-lock.json which locks exact versions

Installing a Development Dependency

Some packages are only needed during development and should not be included in the production bundle:

bash
npm install --save-dev nodemon

This adds the package under devDependencies in package.json.


Understanding Dependency Types

CommandSection in package.jsonPurpose
npm install <pkg>dependenciesRequired for the app to run in production
npm install --save-dev <pkg>devDependenciesOnly needed during development

Common Production Packages

PackagePurpose
expressWeb framework for building servers and APIs
dotenvLoad environment variables from .env files
mongooseODM for working with MongoDB
corsEnable cross-origin resource sharing
axiosHTTP client for making API requests

Common Development Packages

PackagePurpose
nodemonAuto-restart server when files change
eslintJavaScript linter for code quality
jestTesting framework
webpackModule bundler for frontend assets

Project Structure After Initialization

md
my-app/
├── node_modules/        # All installed packages (never commit this)
├── index.js             # Your application entry point
├── package.json         # Project metadata and dependencies
└── package-lock.json    # Locked dependency tree

The node_modules folder should always be added to .gitignore since it can be regenerated from package.json using npm install.


Step 3: Create and Run Your First App

Create the entry point file:

bash
touch index.js

Add the following code to index.js:

javascript
const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello, World!');
});

const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

Run the application:

bash
node index.js

Open your browser and navigate to http://localhost:3000 — you should see Hello, World!


Understanding the Code

md
const express = require('express');

Import the Express module using CommonJS require.

md
const app = express();

Create an Express application instance.

md
app.get('/', (req, res) => { ... });

Define a route. When a GET request arrives at /, execute the callback. req is the request object, res is the response object.

md
app.listen(PORT, callback);

Start the HTTP server and bind it to the specified port.


Using npm Scripts

Instead of typing node index.js every time, define scripts in package.json:

json
{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
}

Now you can run:

bash
npm start        # Run in production mode
npm run dev      # Run in development mode with auto-restart

npm scripts are powerful. They can chain commands, set environment variables, and run complex build pipelines — all from a single short command.


Using nodemon for Development

Restarting the server manually after every change is tedious. nodemon watches your files and automatically restarts the server when changes are detected:

bash
npm install --save-dev nodemon

Update your package.json:

json
{
  "scripts": {
    "dev": "nodemon index.js"
  }
}

Run the development server:

bash
npm run dev

Now every time you save a file, the server restarts automatically.


Managing Environment Variables

Hardcoding values like port numbers or database URLs in your source code is a bad practice. Use environment variables to keep sensitive configuration outside your code.

Install the dotenv package:

bash
npm install dotenv

Create a .env file in the project root:

bash
PORT=3000
DB_URL=mongodb://localhost:27017/mydb

Load environment variables in your app:

javascript
require('dotenv').config();
const express = require('express');
const app = express();

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Hello from Node.js!');
});

app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});

Add .env to your .gitignore to prevent secrets from being committed to version control.


The package-lock.json File

When you install packages, npm generates a package-lock.json file. This file:

  • Records the exact version of every installed package and its sub-dependencies
  • Ensures that everyone on the team installs identical dependency trees
  • Should always be committed to version control

md
package.json          → Declares what your project needs
package-lock.json     → Records exactly what was installed
node_modules/         → Where packages are installed (not committed)


Reinstalling Dependencies

When cloning a project or setting up a new environment, restore all dependencies with:

bash
npm install

npm reads package.json (or package-lock.json) and installs everything automatically.


Final Thoughts

Setting up a Node.js project takes only a few commands. The real power comes from the npm ecosystem — thousands of packages available to solve virtually any problem.

The workflow is straightforward:

  1. npm init -y to create the project
  2. npm install <package> to add dependencies
  3. Write your code in index.js
  4. Define npm scripts for convenient execution
  5. Use nodemon during development for automatic restarts

With this foundation, you are ready to build anything from simple APIs to full-featured web applications.