Confused with relative paths?

Confused with relative paths?

Here’s a list of how directories and file paths work in most programming environments:

Relative Paths

  • ./name.txt:
    Refers to the file name.txt in the current directory.

  • ../name.txt:
    Refers to the file name.txt in the parent directory of the current folder.

  • ../../name.txt:
    Refers to the file name.txt two levels up from the current folder.

  • ./folder/name.txt:
    Refers to the file name.txt inside the subfolder folder in the current directory.

  • ../folder/name.txt:
    Refers to the file name.txt inside the subfolder folder in the parent directory.


Absolute Paths

  • /name.txt:
    Refers to the file name.txt in the root directory (applicable in Unix-like systems).

  • C:\folder\name.txt:
    Refers to the file name.txt in the folder folder on the C: drive (Windows-specific).


Special Cases

  • ~:
    Refers to the home directory of the current user (e.g., /home/username or C:\Users\username).

  • name.txt:
    A plain filename without a relative (./) or absolute (/) prefix usually defaults to current directory, but its resolution depends on the environment.


Path Normalization

Different path notations (/ vs \) matter based on the operating system:

  • Unix/Linux/MacOS: Use / as the path separator.

  • Windows: Use \ as the path separator but most Node.js modules (like path) can handle / as well.