Requiring npm Packages
You'll learn how to load third-party code from npm into your Node.js projects using require().
What Are npm Packages?
npm (Node Package Manager) is a vast ecosystem of reusable JavaScript libraries created by developers worldwide. When you install a package using npm install, it downloads into a special folder called node_modules in your project directory.
Loading Third-Party Modules
Loading an npm package works exactly like requiring built-in modules, but instead of Node.js's internal module name, you use the package name from npm:
// Require the 'lodash' package
const _ = require('lodash');
// Require the 'express' package
const express = require('express');
// Require the 'axios' package
const axios = require('axios');
Notice there's no ./ prefix or file path — just the package name as a string.
How Node.js Finds the Package
When you call require('package-name'), Node.js searches for a folder named package-name inside your node_modules directory. Think of node_modules like a library warehouse where Node.js automatically looks for any non-built-in, non-relative module names.
Example: Using a Third-Party Package
// After running: npm install chalk
const chalk = require('chalk');
console.log(chalk.blue('Hello from chalk!'));
The module name must match exactly what you installed. If you run npm install moment, you require it as require('moment') — not require('Moment') or require('./moment').
Key Takeaway: Pass the npm package name directly to require() (like require('lodash')) to load third-party modules from the node_modules folder — no paths needed.