Relative vs Non-Relative Imports
You'll learn: How TypeScript treats imports that start with ./ or ../ differently from bare module names, and why this distinction matters for module resolution.
Two Kinds of Import Paths
When you write an import statement, TypeScript looks at the path string to decide how to find that module. There are two categories:
Relative imports start with ./, ../, or /:
./utils— same directory../config— parent directory/absolute/path— root-relative (rare)
Non-relative imports are everything else:
reactlodash@company/shared-lib
How TypeScript Treats Each Type
Relative imports are resolved relative to the importing file's location. TypeScript looks at the folder containing your current file and navigates from there. These imports are location-dependent — if you move the importing file, the relative path might break.
Non-relative imports are resolved against configured paths. TypeScript searches:
- The
node_modulesfolder hierarchy - Custom paths defined in
tsconfig.json(likepathsorbaseUrl) - Ambient module declarations
Think of relative imports like directions from your current position ("go two blocks north"), while non-relative imports are like looking up a business name in a directory ("find the nearest Starbucks").
Why the Distinction Matters
Relative imports are typically used for your own code — files within your project that have a known relationship to each other.
Non-relative imports are used for external modules (npm packages) or internal modules configured to act like packages (via paths in tsconfig.json).
The resolution strategy TypeScript chooses depends entirely on this distinction, affecting which folders it searches and in what order.
Key Takeaway: Relative imports (./, ../) resolve from the current file's location, while non-relative imports (bare names) resolve through node_modules or configured module paths — choose based on whether you're importing project files or packages.