DEV Community

Cover image for Day 22 of #100DaysOfCode — Node.js Fundamentals + Understanding NPM
M Saad Ahmad
M Saad Ahmad

Posted on

Day 22 of #100DaysOfCode — Node.js Fundamentals + Understanding NPM

JavaScript started as a language meant only for the browser. Browsers run JavaScript using a built-in engine that understands and executes the code.
But what if you want to run JavaScript outside the browser?

That’s where Node.js comes in.

Node.js takes the same engine used inside Chrome — Google’s V8 engine — and places it on your computer or server.
This lets you run JavaScript without a browser, making it possible to build APIs, servers, CLIs, scripts, and backend applications.


⭐ What Even Is Node.js?

Think of it like this:

  • Browser = JavaScript + UI (DOM, buttons, windows)
  • Node.js = JavaScript + System Access (files, servers, databases, networks)

⚙️ How Node.js Executes JavaScript (Simple Breakdown)

When you run:

node app.js
Enter fullscreen mode Exit fullscreen mode

This is what actually happens:

1️⃣ Node reads your JavaScript file

It loads the code into memory.

2️⃣ V8 compiles your JavaScript

V8 converts JS → optimized machine code → runs on your CPU.

3️⃣ Node’s Event Loop starts

This is what makes Node powerful.
Instead of pausing for slow tasks (files, DB queries, network calls), Node continues executing other code.

4️⃣ libuv takes care of the heavy lifting

libuv is a C++ library that gives Node:

  • Threads
  • File system access
  • Networking
  • Timers
  • Asynchronous I/O

The key concept:

Node uses one main thread, but thanks to the event loop + libuv, it can handle thousands of concurrent tasks without freezing.

Node doesn’t freeze while waiting for slow tasks like database queries or file reads — it moves on and returns later when the task finishes.


📦 Built-In Node Modules (No Installation Needed)

Node comes with a set of core modules that you can use instantly.

Module What It Does
fs Read/write files
path Work with file paths
http Build basic web servers
https Secure web servers (SSL/TLS)
os Info about OS (memory, CPU)
events EventEmitter for custom events
stream Process streaming data
crypto Encryption, hashing, tokens
url Parse and format URLs
buffer Work with binary data
process Info about the running process
child_process Run system commands
timers setTimeout, setInterval

Usage Example

// CommonJS
const fs = require('fs');

// ES Modules
import fs from 'fs';

const data = fs.readFileSync('file.txt', 'utf8');
Enter fullscreen mode Exit fullscreen mode

📥 What Is NPM?

NPM = Node Package Manager
It comes installed automatically with Node.js.

NPM lets you:

  • download reusable packages
  • manage dependencies
  • share your own packages

Think of it like an app store for JavaScript libraries — except everything is code.


📦 What Exactly Is a “Package”?

A package is simply a folder of code published to NPM.

Examples of popular ones:

  • express — server framework
  • lodash — utility helpers
  • axios — HTTP requests
  • dotenv — manages environment variables

NPM hosts over 2 million packages at https://npmjs.com.


❤️ package.json — The Heart of Every Node Project

This file describes your project and keeps track of:

  • Your project info
  • Dependencies
  • Dev dependencies
  • Scripts
  • Configurations

Example:

{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^3.0.0"
  },
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

🔒 package-lock.json — Why It Matters

It is auto-generated when you run npm install.
It stores the exact versions of everything installed.

Simple Analogy to understand package.json and package-lock.json

  • package.json = "I need flour (about 2 cups)"
  • package-lock.json = "I need exactly 2.25 cups of King Arthur bread flour, batch #4821"

This ensures every developer gets identical installs.


📂 dependencies vs devDependencies

dependencies

Needed for the project to run in production.

"dependencies": {
  "express": "^4.18.2",
  "mongoose": "^7.0.0",
  "axios": "^1.4.0"
}
Enter fullscreen mode Exit fullscreen mode

Install:

npm install express
Enter fullscreen mode Exit fullscreen mode

devDependencies

Needed only during development.

"devDependencies": {
  "nodemon": "^3.0.0",
  "jest": "^29.0.0",
  "eslint": "^8.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Install:

npm install nodemon --save-dev
Enter fullscreen mode Exit fullscreen mode

🔧 Essential NPM Commands

Initialize a project

npm init
npm init -y
Enter fullscreen mode Exit fullscreen mode

Install packages

npm install express
npm install nodemon --save-dev
npm install
Enter fullscreen mode Exit fullscreen mode

Remove packages

npm uninstall express
npm uninstall -g nodemon
Enter fullscreen mode Exit fullscreen mode

Run scripts

npm start
npm run dev
npm test
Enter fullscreen mode Exit fullscreen mode

Note: start and test are special — no run required.

Checking & Updating

npm list
npm list -g
npm outdated
npm update
npm update express
Enter fullscreen mode Exit fullscreen mode

Other useful commands

npm --version
npm search express
npm info express
Enter fullscreen mode Exit fullscreen mode

npx — run a package without installing it

npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode

📁 node_modules & .gitignore (Important!)

When you install packages, npm creates the node_modules folder.
It contains thousands of files and should NEVER be pushed to GitHub.

Create a .gitignore and add:

node_modules/
Enter fullscreen mode Exit fullscreen mode

Anyone cloning your repo will simply run:

npm install
Enter fullscreen mode Exit fullscreen mode

And everything will be downloaded automatically.


🧠 Summary

Here’s everything from today in one quick recap:

  • Node.js = JavaScript powered by V8 outside the browser
  • V8 = JS → machine code
  • Event Loop = async, non-blocking magic
  • Core Modules = built-in tools (fs, http, crypto, etc.)
  • NPM = install & manage JavaScript packages
  • package.json = project & dependency metadata
  • package-lock.json = exact versions for reproducibility
  • node_modules = downloaded packages
  • npx = run packages without installing

Happy learning (and coding)!

Top comments (2)

Collapse
 
harsh2644 profile image
Harsh

Day 22! 🔥 That consistency is no joke. Most people quit by Day 7, and here you are building in public for 3 weeks straight.
Node.js fundamentals + NPM is literally the foundation of modern backend. Getting this right now will save you months of confusion later.
Are you planning to cover Express after this, or diving deeper into core Node first? 👨‍💻

Collapse
 
m_saad_ahmad profile image
M Saad Ahmad • Edited

Consistency is the key to move forward, even just 1% every day. After this, it will be ExpressJS. The idea is get the exposure first, then deep dive at the time of project building.