Node.js Path Module

Summary: in this tutorial, you will learn about the path module in Node.js

Node.js offers a path module that allows you to interact with file paths easily.

The path module has many useful properties and methods to access and manipulate paths in the file system.

The path is a core module in Node.js, therefore, you can use it without installing:

const path = require('path');Code language: JavaScript (javascript)

If you use the ES module, you can import the path as follows:

import path from 'path';Code language: JavaScript (javascript)

Useful path properties

The path object has the sep property that represents the platform-specific path separator:

path.sepCode language: JavaScript (javascript)

The path.sep returns \ on Windows and / on Linux and macOS.

The path object also has the delimiter property that represents the path delimiter:

path.delimiterCode language: JavaScript (javascript)

The path.delimiter returns ; on Windows and : on Linux and macOS.

Handy path methods

The following shows some handy methods for the path module that you probably use very often:

path.basename(path, [,ext])
path.dirname(path)
path.extname(path)
path.format(pathObj)
path.isAbsolute(path)
path.join(...path)
path.normalize(path)
path.parse(path)
path.relative(from, to)
path.resolve(...path)Code language: JavaScript (javascript)

path.basename(path[, ext])

The path.basename() returns the last portion of a specified path. For example:

import path from 'path';

let result = path.basename('/public_html/home/index.html');
console.log(result);Code language: JavaScript (javascript)

Output:

index.htmlCode language: JavaScript (javascript)

The ext parameter filters out the extension from the path:

import path from 'path';

let result = path.basename('/public_html/home/index.html', '.html');
console.log(result);Code language: JavaScript (javascript)

Output:

indexCode language: JavaScript (javascript)

path.dirname(path)

The path.dirname() method returns the directory name of a specified path. For example:

import path from 'path';

let result = path.dirname('/public_html/home/index.html');
console.log(result);Code language: JavaScript (javascript)

Output:

/public_html/homeCode language: JavaScript (javascript)

Note that the path.dirname() ignores the trailing directory.

path.extname(path)

The path.extname() returns extension of the path. For example:

import path from 'path';

console.log(path.extname('index.html'));
console.log(path.extname('app.js'));
console.log(path.extname('node.js.md'));Code language: JavaScript (javascript)

Output:

.html
.js
.mdCode language: JavaScript (javascript)

path.format(pathObj)

The path.format() method returns a path string from a specified path object.

import path from 'path';

let pathToFile = path.format({
  dir: 'public_html/home/js',
  base: 'app.js',
});

console.log(pathToFile);
Code language: JavaScript (javascript)

Output (on Linux or macOS):

public_html/home/js/app.js
Code language: JavaScript (javascript)

path.isAbsolute(path)

The path.isAbsolute() returns true if a specified path is an absolute path.

For example, on Windows:

import path from 'path';

let result = path.isAbsolute('C:\\node.js\\');
console.log(result); // true

result = path.isAbsolute('C:/node.js/');
console.log(result); // true

result = path.isAbsolute('/node.js');
console.log(result); // true

result = path.isAbsolute('home/');
console.log(result); // false

result = path.isAbsolute('.');
console.log(result); // false
Code language: JavaScript (javascript)

On Linux & macOS:

import path from 'path';

let result = path.isAbsolute('/node/js/');
console.log(result); // true

result = path.isAbsolute('/node/..');
console.log(result); // true

result = path.isAbsolute('node/');
console.log(result); // false

result = path.isAbsolute('.');
console.log(result); // falseCode language: JavaScript (javascript)

path.join(…paths)

The path.join() method does two things:

  • Join a sequence of path segments using the platform-specific separator as a delimiter
  • Normalize the resulting path and return it.

For example:

import path from 'path';

let pathToDir = path.join('/home', 'js', 'dist', 'app.js');
console.log(pathToDir);Code language: JavaScript (javascript)

Output (on Windows):

\home\js\dist\app.jsCode language: JavaScript (javascript)

path.parse(path)

The path.parse() method returns an object whose properties represent the path elements. The returned object has the following properties:

  • root: the root
  • dir: the directory path from the root
  • base: the file name + extension
  • name: the file name
  • ext: the extension