- Node.js Tutorial
- 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 Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - path.parse() Method
The Node.js path.parse() method of the path module lists the properties of the given path as an object. The following are the object properties that represent the given path.
dir − This property specifies the directory name of the given path.
root − This property specifies the root name of the given path.
base − This property specifies the file name with the extension of the given path.
name − This property specifies the file name of the given path.
ext − This property specifies only the extension of the given path.
The values of the returned object properties may vary for different platforms such as POSIX. This method ignores the trailing directory separators.
Syntax
Following is the syntax of the Node.js path.parse() method of the path module.
path.parse(path)
Parameters
path − This parameter specifies the path that would be parsed by the method. A TypeError will be thrown if the path is not a string.
Return value
This method returns an object containing properties, that specify the segments of the given path.
Example
If a completely normalized path is passed to this method, all the properties which represent the given path will be returned as an object.
In the following example, we are providing all the properties that are 'root', 'dir', 'base', 'ext', and 'name' to the method.
const path = require('path'); var result = path.parse("C:/Users/Lenovo/Desktop/JavaScript/nodefile.js"); console.log(result);
Output
After executing the above program in online compiler (POSIX), the method returns all the path properties as an object.
{ root: '', dir: 'C:/Users/Lenovo/Desktop/JavaScript', base: 'nodefile.js', ext: '.js', name: 'nodefile' }
Following is the output when we execute the above code on WINDOWS operating system.
{ root: 'C:/', dir: 'C:/Users/Lenovo/Desktop/JavaScript', base: 'nodefile.js', ext: '.js', name: 'nodefile' }
Example
If an empty path is passed to this method, the properties of the returned object will be empty.
In the following example, we are passing an empty path to the path.parse() method.
const path = require('path'); var result = path.parse(""); console.log(result);
Output
After executing the above program, the path.parse() method returns empty object properties.
{ root: '', dir: '', base: '', ext: '', name: '' }
Example
If any path segment of the path is not given to this method, that particular path segment property of the returned object will be empty.
In the given program, we are trying to pass the path without a root segment.
const path = require('path'); var result = path.parse("JavaScript/nodefile.js"); console.log(result);
Output
If we compile and run the above program, the method returns the 'root' property as empty because there is no 'root' segment given in the given path.
{ root: '', dir: 'JavaScript', base: 'nodefile.js', ext: '.js', name: 'nodefile' }
Example
If we pass a value that is not a string type to the path parameter, this method will throw a TypeError.
In the given example, we are passing an instance of an object instead of a string to the path parameter of the method.
const path = require('path'); var result = path.parse({}); console.log(result);
TypeError
After executing the above program, the path.isAbsolute() method throws a TypeError as the path parameter is not a string value.
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 object at assertPath (path.js:39:11) at Object.parse (path.js:1436:5) at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:19) 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)