Node.js: Code Outside the Browser / Working with npm (Node Package Manager)

Our Own Modules

πŸ‘·β€β™‚οΈ Setup

  1. πŸ“₯ Grab the starter files for this section and open the project folder.

    Download the Repo & Open starter folder inside : 1-node-farm
    https://github.com/codevedadev/complete-node-bootcamp

  2. πŸ“„ Create a new file: index.js

  3. πŸ“ If you already have it ready, skip this step.


πŸ€” Why Do This?

  • πŸ“„ Right now, the replaceTemplate function lives directly inside index.js.

  • πŸ”„ It's used twice there (once for the overview cards, once for the product page).

  • πŸ“š But imagine a bigger project with many files that all need to use this same function. Copy-pasting it everywhere would be messy and hard to maintain.

  • 🧩 The solution: put the function in its own file (module), export it from there, and import it wherever it's needed.


🧩 Key Concept: Every File Is a Module

  • πŸ“ In Node.js, every single .js file is treated as its own module.

  • πŸ“„ index.js itself is a module β€” and it already imports other modules (like fs, http, url).

  • πŸ› οΈ Now we'll create our own custom module, just like those built-in ones.


πŸ› οΈπŸ“‚ Step 1: Create the Module File

  1. πŸ“ Create a new folder called modules (⚠️ not modules.js β€” just modules, a plain folder).

  2. πŸ“„ Inside it, create a file called replaceTemplate.js.

  1. βœ‚οΈ Cut the replaceTemplate function from index.js and πŸ“‹ paste it into this new file.


πŸ“€  Step 2: Export the Function from the Module

  • 🧩 Every Node.js module automatically has access to a special object called module.

  • πŸ“€ To make something available to other files, assign it to module.exports:

module.exports = (temp, product) => {

πŸ“– Explanation

  • 🧩 module is a built-in object available in every Node.js file/module.

  • πŸ“€ Setting module.exports to something (in this case, the function itself) tells Node: "this is what other files get when they require() this module."

  • 🏷️ The function is now anonymous (it no longer needs a name like replaceTemplate inside this file) β€” its name will instead be decided by whoever imports it.

πŸ’‘ Deeper details on different ways of exporting from modules will be covered in a later section of the course β€” for now, this pattern (module.exports = ...) is enough.


πŸ“₯  Step 3: Import the Function into index.js

  • πŸ“„ Back in index.js, add a require() statement near the top:

const replaceTemplate = require('./modules/replaceTemplate');

πŸ“– Explanation

  • Import order convention: core/built-in modules (fs, http, url) are typically required first, followed by your own custom modules.

  • ./modules/replaceTemplate:

    • ./ means "current folder" β€” but this is a special exception: normally, . in file paths (like in fs.readFileSync) refers to wherever the node command was run from. Inside require(), though, . always means the location of the current file/module, not the terminal's working directory.

    • No .js extension is needed β€” Node resolves it automatically.

  • Whatever was assigned to module.exports in replaceTemplate.js (the function) is what this require() call returns.

  • The result is stored in a variable β€” here still named replaceTemplate, so no other code needs to change.

Tip: keeping the imported variable name the same keeps the rest of your code cleaner and avoids unnecessary changes elsewhere.


πŸ§ͺ Step 4: Verifying It Still Works

  • 🌐Restarting the server, both the overview page and product page continue to work exactly as before β€” because functionally, nothing changed.

  • πŸ“¦ The replaceTemplate function just now lives in its own file and gets imported rather than being defined locally.


πŸŽ‰ Project Status: Complete!

This wraps up the Node Farm project. You now understand:

  • πŸ“– Reading/writing files (sync & async)

  • 🌐 Building a basic web server

  • πŸ›£οΈ Implementing routing

  • πŸ”Œ Building a simple API

  • πŸ“„ Creating and filling HTML templates dynamically

  • πŸ”— Parsing URLs and query strings

  • 🧩 Creating and using your own custom modules


πŸŽ―πŸ“š Key Takeaways

πŸ“Œ Concept

πŸ’‘ Explanation

πŸ“„ Every .js file = a module

Node treats each file as its own self-contained module

πŸ“€ module.exports = ...

Assigns whatever you want to make available to other files (function, object, value, etc.)

πŸ“₯ require('./path/to/file')

Imports whatever was exported from that module β€” no .js extension needed

πŸ“ . inside require()

Special exception: always means "the current module's folder," unlike . elsewhere (e.g., in fs functions), which means "the terminal's working directory"

πŸ“‚ Folder naming matters

A folder named modules.js (with .js) breaks path resolution β€” keep folder names extension-free

πŸ“¦ Import order convention

Core Node modules first, then your own custom modules