Working with Files: Node File Paths

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Every file in the system has a path.

On Linux and macOS, a path might look like:

/users/flavio/file.txt

while Windows computers are different, and have a structure such as:

C:\users\flavio\file.txt

You need to pay attention when using paths in your applications, as this difference must be taken into account.

You include this module in your files using

const path = require('path')

and you can start using its methods.

Getting information out of a path

Given a path, you can extract information out of it using those methods:

  • dirname: get the parent folder of a file
  • basename: get the filename part
  • extname: get the file extension

Example:

const notes = '/users/flavio/notes.txt'

path.dirname(notes) // /users/flavio
path.basename(notes) // notes.txt
path.extname(notes) // .txt

You can get the file name without the extension by specifying a second argument to basename:

path.basename(notes, path.extname(notes)) //notes

Working with paths

You can join two or more parts of a path by using path.join():

const name = 'flavio'
path.join('/', 'users', name, 'notes.txt') //'/users/flavio/notes.txt'

You can get the absolute path calculation of a relative path using path.resolve():

path.resolve('flavio.txt') //'/Users/flavio/flavio.txt' if run from my home folder

In this case Node will append /flavio.txt to the current working directory. If you specify a second parameter folder, resolve will use the first as a base for the second:

path.resolve('tmp', 'flavio.txt')//'/Users/flavio/tmp/flavio.txt' if run from my home folder

If the first parameter starts with a slash, that means it’s an absolute path:

path.resolve('/etc', 'flavio.txt')//'/etc/flavio.txt'

path.normalize() is another useful function, that will try and calculate the actual path, when it contains relative specifiers like . or .., or double slashes:

path.normalize('/users/flavio/..//test.txt') ///users/test.txt

Both resolve and normalize will not check if the path exists. They just calculate a path based on the information they got.

Lessons in this unit:

0: Introduction
1: How to check if a file exists in Node.js
2: How to create an empty file in Node.js
3: How to get the names of all the files in a folder in Node
4: How to get the last updated date of a file using Node.js
5: How to remove a file with Node.js
6: How to write a JSON object to file in Node.js
7: How to empty a folder in Node.js
8: Working with file descriptors in Node
9: ▶︎ Node File Paths
10: Node file stats
11: Working with folders in Node
12: How to use the Node.js fs module with async/await
13: How to get the current folder in Node
14: How to get the file extension in Node.js from the MIME type
15: How to get the file extension in Node.js
16: How to list files in a folder in Node
17: How to mass rename files in Node.js
18: Node.js get all files in a folder recursively
19: Incrementing multiple folders numbers at once using Node.js
20: Save some text to a file in Node.js