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

3rd Party Modules

🎯 Goal of This Note

Learn how to require and use a third-party npm package (slugify) in code β€” completing the picture alongside core modules (like fs, http) and custom modules (like replaceTemplate).


πŸ”„ Recap: Three Types of require() Imports

πŸ“Œ Imports are conventionally placed at the top of the file, in this order:

  1. βš™οΈ Core Node.js modules (e.g., fs, http, url)

  2. πŸ“¦ Third-party modules installed via npm (e.g., slugify) ← this note's focus

  3. 🧩 Your own custom modules (e.g., replaceTemplate)


πŸ“₯ Step 1: Requiring a Third-Party Package

const slugify = require('slugify');

πŸ” How This Works

  • πŸ“¦ Just like requiring a core module, you use require() with the package name.

  • πŸ“‚ Since slugify isn't a built-in Node module, Node automatically knows to look inside the node_modules folder (created earlier when you ran npm install slugify) to find it.

  • 🏷️ The result β€” a function, in this case β€” is stored in a variable (named slugify here, matching the package name for clarity).


πŸ”— Step 2: What Is a "Slug"?

  • πŸ“ A slug is the human-readable, unique part of a URL that identifies a specific resource β€” commonly seen in blog posts and product pages.

πŸ’‘ Example

  • ❌ Instead of a URL like:

/product?id=0

You could have something more meaningful like:

/product/fresh-avocados

Here, fresh-avocados is the slug β€” generated from a name like "Fresh avocados" by:

  • Converting it to lowercase.

  • Replacing spaces with dashes.

  • This is much more descriptive and SEO-friendly than a plain numeric ID.


πŸ“– Step 3: Checking the Documentation

  • πŸ’‘ When using a new package for the first time, it's good practice to check its documentation on npmjs.com:

  1. Search for the package by name (slugify) on npmjs.com.

  2. The docs show usage examples, such as:

slugify(string, options);
  1. βš™οΈ Available options include things like:

  • πŸ”„ replacement β€” the character used to replace spaces (defaults to , but could be _ or nothing).

  • πŸ”‘ lower β€” whether to convert everything to lowercase (true/false).

πŸ’‘ Lesson: Documentation is essential for correctly understanding a package's options and behavior β€” don't guess, check the docs.


πŸ§ͺ Step 4: Testing Slugify

console.log(slugify('Fresh avocados', { lower: true }));
  • πŸ”„ This converts "Fresh avocados" into a clean slug: fresh-avocados.

πŸ’‘ Bonus tip: Since Nodemon is now running (from the previous lecture), there's no need to manually stop and restart the server after each code change β€” Nodemon automatically restarts it and shows the updated console output.


🏷️ Step 5: Generating Slugs for All Products

  • πŸ“¦ Using the array of products (dataObject) and the familiar .map() method:

const slugs = dataObj.map(el => slugify(el.productName, { lower: true }));
console.log(slugs);

πŸ” How This Works

  • πŸ” .map() loops through each product object (el) in dataObject.

  • 🏷️ For each one, it creates a slug from el.productName (the field holding the product's name).

  • πŸ“‹ The result is a new array of slug strings β€” one for each of the five products.


πŸ“¦ Key Concept: This Package Is Now a Dependency

  • By using slugify in the code, the project now genuinely depends on it β€” without this package installed, this part of the code would break.

  • This is exactly why npm tracks it under "dependencies" in package.json.


🚫 What Wasn't Done (On Purpose)

  • The lecture stops short of actually:

    • πŸ’Ύ Saving these generated slugs into data.json.

    • πŸ”— Updating the app's routing/templates to use slugs instead of numeric IDs in URLs.

  • This is intentional β€” doing so would just be repeating the same concepts already learned (modifying JSON, updating routes, templates, etc.) without introducing anything new.

  • The purpose of this lecture was purely to demonstrate the process of integrating a third-party npm package:

    1. πŸ“₯ Install it (npm install).

    2. πŸ“¦ require() it in code.

    3. πŸ“– Check its documentation.

    4. πŸš€ Use it with a simple example.


🎯Key Takeaways

πŸ“Œ Concept

πŸ’‘ Explanation

πŸ“₯ require('package-name')

Loads a third-party package from node_modules (installed via npm)

πŸ“¦ Import order convention

Core modules β†’ third-party modules β†’ your own custom modules

🏷️ Slug

A readable, unique URL string (e.g., fresh-avocados) β€” more meaningful than a plain ID

πŸ“– Checking documentation

Essential for correctly using unfamiliar packages and their options (e.g., lower vs. lowercase)

πŸ” .map() with a package function

Common pattern: transform an array of data using a third-party function inside the callback

βœ… A used package = a dependency

Once your code relies on a package to function, it becomes a real dependency of your project

➑️ This completes the module import trilogy:

  • βš™οΈ Core modules

  • 🧩 Own modules

  • πŸ“¦ Third-party modules

all imported using the same require() function, just resolving to different locations (Node's built-ins, your local files, or node_modules).