Node.js File Paths

Every file in the system has a path. On Linux and macOS, a path might look like: /users/joe/file.txt while Windows computers are different, and have a structure such as: C:\users\joe\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('node: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: gets the parent folder of a file
  • basename: gets the filename part
  • extname: gets the file extension

Example

const const path: path.PlatformPathpath = 
var require: NodeJS.Require
(id: string) => any
Used to import modules, `JSON`, and local files.
@sincev0.1.13
require
('node:path');
const const notes: "/users/joe/notes.txt"notes = '/users/joe/notes.txt'; const path: path.PlatformPathpath.path.PlatformPath.dirname(path: string): string
Return the directory name of a path. Similar to the Unix dirname command.
@parampath the path to evaluate.@throws{TypeError} if `path` is not a string.
dirname
(const notes: "/users/joe/notes.txt"notes); // /users/joe
const path: path.PlatformPathpath.path.PlatformPath.basename(path: string, suffix?: string): string
Return the last portion of a path. Similar to the Unix basename command. Often used to extract the file name from a fully qualified path.
@parampath the path to evaluate.@paramsuffix optionally, an extension to remove from the result.@throws{TypeError} if `path` is not a string or if `ext` is given and is not a string.
basename
(const notes: "/users/joe/notes.txt"notes); // notes.txt
const path: path.PlatformPathpath.path.PlatformPath.extname(path: string): string
Return the extension of the path, from the last '.' to end of string in the last portion of the path. If there is no '.' in the last portion of the path or the first character of it is '.', then it returns an empty string.
@parampath the path to evaluate.@throws{TypeError} if `path` is not a string.
extname
(const notes: "/users/joe/notes.txt"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 const name: "joe"name = 'joe';
path.join('/', 'users', const name: "joe"name, 'notes.txt'); // '/users/joe/notes.txt'

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

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

In this case Node.js will simply append /joe.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', 'joe.txt'); // '/Users/joe/tmp/joe.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', 'joe.txt'); // '/etc/joe.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/joe/..//test.txt'); // '/users/test.txt'

Neither resolve nor normalize will check if the path exists. They just calculate a path based on the information they got.

Temps de Lecture
1 min
Auteurs
Contribuer
Éditer cette page
Table des matières
  1. Getting information out of a path
  2. Example
  3. Working with paths