Course contentsShow
← TypeScript
Lesson 1113 of 1,79626. Modules in TypeScriptFree lesson

Relative vs Non-Relative Imports

How TypeScript treats ./foo vs foo imports differently during resolution.

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:

  • react
  • lodash
  • @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:

  1. The node_modules folder hierarchy
  2. Custom paths defined in tsconfig.json (like paths or baseUrl)
  3. 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.