Module Paths and Extensions
You'll learn how to correctly specify where your modules live and when file extensions are required.
The Core Idea
When you import a module, JavaScript needs to know where to find it. Module paths tell the engine whether to look in your project files, and whether to treat the path as relative to the current file or as an absolute location.
Relative Paths
Relative paths start with ./ (current directory) or ../ (parent directory):
import { helper } from './utils.js'; // Same folder
import { config } from '../config/app.js'; // Parent, then config folder
Think of ./ as "right here" and ../ as "go up one level first."
Absolute Paths
Absolute paths start from the project root or use full URLs:
import { api } from '/src/services/api.js'; // From root
In browsers, you can even import from URLs:
import { library } from 'https://cdn.example.com/lib.js';
File Extensions
In ES modules, you usually must include .js extensions when importing your own files:
import { data } from './data.js'; // ✓ Works
import { data } from './data'; // ✗ May fail in browsers
Node.js traditionally allowed omitting extensions, but ES modules made them more important for clarity and performance.
Why It Matters
Explicit paths and extensions eliminate ambiguity. The module loader doesn't have to guess or search multiple locations, making imports faster and more predictable.
Key Takeaway: Always use relative paths (./ or ../) for local modules, include the .js extension for clarity and compatibility, and remember that absolute paths start from your project root or use full URLs.