Understanding npm and Node.js Basics – A Beginner’s Guide to File System and HTTP in JavaScript

Yukti SahuYukti Sahu
3 min read

Series: Backend Dev Diaries: JS Edition

This is Part 2 of my learning-in-public journey into backend development using JavaScript.

🧰 What is npm? And Why Does It Matter?

npm stands for Node Package Manager. It’s a tool that helps developers manage packages (or modules) in their JavaScript projects.

Think of it like the Play Store, but for JavaScript code. You can install useful tools, libraries, or frameworks in your project with just one line!

👤 Who created npm?

It was created by Isaac Z. Schlueter in 2010, the same year Node.js started gaining popularity.
Since then, it has become the largest software registry in the world.


📦 How to Initialize a Node.js Project?

To start any backend project in Node.js, you first need to create a package.json file. It’s like your project’s ID card — it keeps track of:

  • The name and version of your app

  • What dependencies (npm packages) you’re using

  • Scripts to run your app

🧪 Commands to initialize:

npm init        # step-by-step setup
npm init -y    # shortcut for default settings

This creates a package.json file. You’ll find all your project’s details here.


📚 Using npm Packages/Modules

There are two types of modules:

  • Built-in (like fs, http)

  • External (like express, mongoose)

You can use any module by requiring it in your file:

const fs = require('fs'); // File System module

Once imported, all the features of the module can be used via fs.

You can always explore module documentation to understand more. In a Node.js project, check:

node_modules/ → all your installed packages
package.json → records what you’re using

✍️ Basic File Operations with the fs Module

Here are some real things you can do with the fs module (File System):

✅ Write to a file:

fs.writeFile('hello.txt', "Hey hello", (err) => {
    if (err) console.log("Error writing file", err);
    else console.log("File written successfully");
});

➕ Append text:

fs.appendFile('hello.txt', " Yukti ji0", (err) => {
    if (err) console.log("Error appending");
    else console.log("Content added");
});

📝 Read from a file:

fs.readFile('hello.txt', 'utf-8', (err, data) => {
    if (err) console.log(err.message);
    else console.log(data);
});

🧼 Delete a file:

fs.unlink('hello.txt', (err) => {
    if (err) console.log(err.message);
    else console.log("File deleted");
});

📁 Create a folder:

fs.mkdir('./folderName', (err) => {
    if (err) console.log(err.message);
    else console.log("Directory created");
});

❌ Remove a folder:

fs.rm('./folderName', { recursive: true }, (err) => {
    if (err) console.log(err.message);
    else console.log("Directory deleted");
});

🌐 What is the HTTP Module?

To send and receive data on the web, we use HTTP — it's a rule or protocol.
Node.js gives us a built-in http module to create web servers without any external tools.

Here’s how simple it is:

const http = require('http');

const server = http.createServer(function (req, res) {
    res.end("How are you doing?");
});

server.listen(3000);

📍 Visit http://localhost:3000 in your browser and see your first Node.js response in action!


🧭 What’s Coming Next?

In the next part of this series, I’ll cover:

  • Understanding npm what it is

  • working with npm

  • installing and uninstalling them

  • packages and scripts


👋 Final Words

This post covered a lot of real-world backend basics — not just theory. You’ve now seen:

  • What npm is and why it exists

  • How to initialize a Node.js project

  • File operations using fs

  • Creating a basic server using http

You’ve already started thinking like a backend dev! 🔥

Let’s keep building step by step.


💬 Drop a comment if something was confusing — or just to say hi!
📌 Follow me for the next article and backend updates every week.


📦 GitHub Repository

Check out all the code examples, practice scripts, and setups here:
🔗 github.com/yukti-says/Backend-Learning

⭐ Don’t forget to star the repo if you found it helpful!

Yukti Sahu

#JavaScript #Nodejs #Backend #npm #DevDiaries #BeginnerFriendly

20
Subscribe to my newsletter

Read articles from Yukti Sahu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yukti Sahu
Yukti Sahu