• Node.js Video Tutorials

Node.js - path.basename() Method



The path module of Node.js provides some useful utilities for working with file and directory paths.

On windows, the Node.js path.basename() method returns the last portion of a specified path. Whereas, on LINUX based systems, we can get the last portion of the path by using basename command. This is useful for extracting names from paths without having to parse them manually. This method ignores the trailing directory separators.

Syntax

Following is the syntax of the Node.js path.basename() method of path module −

path.basename(path[, suffix])

Parameters

This method accepts two parameters. The same are described below.

  • path − This parameter specifies a particular file path, that would be used to extract the filename. This parameter must be passed as a string.

  • suffix − It is an optional parameter which holds the extensions of the file such as .txt or .json, that would be removed from the returned string.

Return value

This method will return a string that specifies the file name part of the file path. A typeError is thrown if the path or the extension parameter are not string values.

Example

If we pass the path parameter to the method, it will return the last portion of the specified path.

In the following example, we are trying to get the file name part by passing the file path to the path parameter of Node.js path.basename() method of path module.

const path = require('path');
var path_of_file = path.basename("/Users/Lenovo/Desktop/JavaScript/Nodefile.js");
console.log(path_of_file);

Output

After executing the above program, the Node.js path.basename() will return the name of the file; including the extension.

Nodefile.js

Example

If we pass both path and suffix to the method, it will return the last portion of the path by removing the suffix.

In the following example, we are trying to get the file name part of the path, without the extension, by passing the extension (".js") to the suffix parameter.

const path = require('path');
var path_of_file = path.basename("/Users/Lenovo/Desktop/JavaScript/Nodefile.js", ".js")
console.log(path_of_file);

Output

After executing the above program, the path.basename() method will return the last portion of the path excluding the suffix.

Nodefile

Example

If we pass a value which is not a string type to the path parameter, this method will throw a TypeError.

In the following example, we are passing integer, instead of a string to the path parameter of the method.

const path = require('path');
var path_of_file = path.basename(098870957214)
console.log(path_of_file);

TypeError

If we compile and run the above program, this method will throw an TypeError.

path.js:39
   throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
   ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number
   at assertPath (path.js:39:11)
   at Object.basename (path.js:1299:5)
   at Object.<anonymous> (/home/cg/root/63a02348bb977/main.js:3:25)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)

Example

If we pass a value which is not a string type to the suffix parameter, the method will throw a TypeError.

In the following example, we are trying to pass integer, instead of string to the suffix.

const path = require('path');

var path_of_file = path.basename("C:/Users/Lenovo/Desktop/JavaScript/Nodefile.js", 986876)

console.log(path_of_file);

TypeError

As we can see in the output below, it will throw an TypeError.

path.js:1298
   throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
   ^

TypeError [ERR_INVALID_ARG_TYPE]: The "ext" argument must be of type string. Received type number
   at Object.basename (path.js:1298:13)
   at Object.<anonymous> (/home/cg/root/63a02348bb977/main.js:3:25)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_path_module.htm
Advertisements