Course contentsShow
← JavaScript
Lesson 1445 of 2,72331. ModulesFree lesson

Relative Paths in Require

Use ./ and ../ to require local files relative to the current module's location.

Relative Paths in Require

What you'll learn: How to use ./ and ../ to require your own local files relative to the current module's location.

When you require npm packages or built-in modules, you just use their name (require('fs') or require('lodash')). But when you want to require your own files, you need to tell Node.js exactly where to find them relative to the file doing the requiring.

The Two Relative Path Prefixes

./ means "start from the current directory" — the folder containing the file that's calling require(). If you have app.js and it needs to require utils.js in the same folder, you write:

const utils = require('./utils');

../ means "go up one directory level". If your file is in src/controllers/userController.js and needs to require src/models/user.js, you'd write:

const User = require('../models/user');

Think of it like giving directions: "Start where you are (./), then move to these folders" or "Go back to the parent folder (../), then navigate from there."

Why This Matters

Without ./ or ../, Node.js assumes you're requiring a package from node_modules or a built-in module. If you wrote require('utils'), Node would search for an npm package called utils, not your local utils.js file sitting right next to your code.

You can chain ../ to go up multiple levels: ../../config/database goes up two folders, then into config.

Key Takeaway: Always prefix local file requires with ./ (same directory) or ../ (parent directory) to tell Node.js you're requiring your own code, not an installed package.