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

Types of Packages and Installs

๐Ÿ“ฆ Two Types of Packages

1. ๐Ÿ“š Regular (Simple) Dependencies

  • Packages that contain code used directly in your application โ€” your code depends on them to run correctly.

  • Example: Express (a Node.js framework used later in the course).

2. ๐Ÿงช Development Dependencies (Dev Dependencies)

  • Tools used only during development โ€” not needed for the app to actually run in production.

  • Examples: code bundlers (like Webpack), debuggers, testing libraries, nodemon.

  • Your code doesn't "depend" on these to function โ€” they just make building the app easier.


๐Ÿ“ฅ Step 1: Installing a Regular Dependency (Slugify)

  • ๐Ÿ”ค Slugify is a small package that converts text (like product names) into clean, URL-friendly strings.

๐Ÿ’ป In VSCode Terminal:

npm install slugify

โœ… What Happens

  • ๐Ÿ“ฆ The package installs immediately.

  • ๐Ÿ“ A new field called "dependencies" appears in package.json, listing slugify and its installed version number.

"dependencies": {
  "slugify": "^1.3.4"
}

๐Ÿ’ก Slugify will actually be used in code in the next lecture.


๐Ÿ› ๏ธ Step 2: Installing a Dev Dependency (Nodemon)

  • โšก Nodemon is a development tool that automatically restarts your Node server whenever you save a change to your files โ€” so you no longer have to manually stop (Ctrl + C) and restart (node index.js) every time you edit code.

๐Ÿ’ป In VSCode Terminal:

npm install nodemon --save-dev

โœ… What Happens

  • ๐Ÿ“„ A new field called "devDependencies" appears in package.json:

"devDependencies": {
  "nodemon": "^1.18.11"
}
  • ๐Ÿงช The -save-dev flag tells npm this is a development dependency, not a regular one.


๐ŸŒ Two Types of Installs: Local vs. Global

๐Ÿ“ Local Install (Default)

  • Both slugify and nodemon were installed locally โ€” meaning they only work inside this specific project folder.

  • npm automatically creates a folder called node_modules in your project, which stores:

    • ๐Ÿ“ฆ The packages you installed directly.

    • ๐Ÿ”— Plus any packages those packages depend on internally (their own dependencies).

  • This is why node_modules often contains many more folders than just the ones you explicitly installed โ€” it's a whole dependency tree.

๐ŸŒ Global Install

  • A global install makes a package available anywhere on your computer, not just inside one project folder.

  • When to install globally: when a package provides a command-line tool/executable you want to use across many projects (like nodemon).

๐Ÿš€ Installing Nodemon Globally:

  • Run this command in VS Code terminal:

npm i nodemon --global

( i is shorthand for install.)

๐ŸŽ Possible Permission Error (Mac)

  • โš ๏ธ On Mac, this command might fail due to lack of permission.

  • ๐Ÿ”‘ Fix: prefix the command with sudo and enter your password (or use Touch ID if available):

sudo npm i nodemon --global
  • ๐ŸชŸ On Windows, this typically works without needing extra permissions.


โšก Step 3: Using Nodemon Instead of node

โฎ๏ธ Before (Manual Restarts Needed)

node index.js
  • ๐Ÿ”„ Every time you changed code, you had to stop the server (Ctrl + C) and restart it manually.

๐Ÿš€ Now (With Nodemon)

nodemon index.js
  • ๐Ÿ‘€ Nodemon watches all files in the project.

  • ๐Ÿ’พ Any time you save a change, the terminal shows:

restarting due to changes...
  • โšก Nodemon automatically re-runs node index.js behind the scenes.

๐Ÿ’ก Tip: You can use Tab to autocomplete filenames in the terminal โ€” e.g., typing i then pressing Tab can auto-complete to index.js if it's the only file starting with "i".

โš™๏ธ What Nodemon Actually Is

  • Nodemon is essentially a wrapper around the normal node command โ€” it just adds the auto-restart convenience on top.

  • From this point in the course onward, nodemon replaces node for running the project during development.


๐Ÿ“œ Step 4: Using a Local Dev Dependency via npm Scripts

โ“ The Problem

  • ๐Ÿšซ If nodemon were only installed locally (not globally), you couldn't run nodemon index.js directly in the terminal โ€” local dependencies aren't accessible as standalone terminal commands.

๐Ÿ’ก The Solution: npm Scripts

  • ๐Ÿ“„ package.json has a "scripts" field.

  • โš™๏ธ When you ran npm init earlier, it auto-generated a placeholder:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
}

You can replace or add your own custom scripts, in package.json file:

"scripts": {
  "start": "nodemon index.js"
}

โ–ถ๏ธ  Running the Script

npm run start
  • This runs the command defined under "start" โ€” in this case, effectively running nodemon index.js.

  • Importantly, this uses the local nodemon (from devDependencies), not the global one โ€” so this works even without a global install.

โšก Shortcut for start

  • "start" is a special, commonly recognized script name in npm.

  • ๐Ÿš€ You can run it with just:

    (No need to type npm run start โ€” start gets a shortcut, unlike custom script names which always need npm run <name>.)

npm start

๐Ÿ“‹ Summary Table

๐Ÿ“Œ Concept

๐Ÿ’ป Command

๐ŸŽฏ Purpose

๐Ÿ“ฆ Install a regular dependency

npm install <package>

Adds to "dependencies" in package.json

๐Ÿ› ๏ธ Install a dev dependency

npm install <package> --save-dev

Adds to "devDependencies" in package.json

๐ŸŒ Install a package globally

npm install <package> --global (or npm i <package> -g)

Makes it available as a CLI command anywhere on your machine

โ–ถ๏ธ Run project normally

node index.js

Standard way to run a script (no auto-restart)

โšก Run project with auto-restart

nodemon index.js

Restarts automatically on file changes (requires Nodemon)

๐Ÿ“œ Run a custom npm script

npm run <script-name>

Executes the command defined in package.json under "scripts"

๐Ÿš€ Run the special start script

npm start

Shortcut โ€” no need for npm run


๐ŸŽฏKey Takeaways

  • ๐Ÿ“ฆ Regular dependencies = code your app needs to run (e.g., Express, Slugify).

  • ๐Ÿ› ๏ธ Dev dependencies = tools that help you build/develop the app but aren't needed in production (e.g., Nodemon).

  • ๐Ÿ“ Local installs only work within the project folder; ๐ŸŒ global installs work as command-line tools anywhere on your machine.

  • ๐Ÿ“‚ node_modules contains not just what you installed, but also all of their dependencies.

  • โšก Nodemon saves time by auto-restarting the server on file changes โ€” a huge quality-of-life improvement during development.

  • ๐Ÿ“œ npm scripts (defined in package.json) let you run local dependencies as if they were global commands, and give you reusable, memorable commands for common tasks (like npm start).

โžก๏ธ Next step in the course: Actually use one of these installed dependencies (Slugify) inside the project's code.