
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
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)