
- 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.relative() method
There are various operating system-related utility methods and properties given in the Node.js path module.
The Node.js path.relative() method of the path module returns a relative path from one described path (from) to another path (to) based on the current working directory. A zero-length string is returned, when from and to each resolve to the same path (after calling path.resolve() on each). If any zero-length strings are passed to the paths, then this method prints a zero-length string. A TypeError will be thrown if either described path is not a string.
Syntax
Following is the syntax of the Node.js path.relative() method of path module −
path.relative(from, to)
Parameters
from − This parameter holds a string file path that represents a base path.
to − This parameter is a string file path that would be used to find the relative path from the base path.
Return value
This method returns the relative path from one specified path from to another specified path to.
Example
The path.relative() method will return the relative path if there is a relative path between the from and to parameters.
In the following example, we are providing the paths to both the from and to parameters of the method.
const path = require('path'); var path1 = path.relative('C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', 'C:/Users/Lenovo/Desktop/JavaScript/file/bose.html') console.log(path1);
Output
Following is the output, when we execute the above code in online compiler (POSIX).
../file/bose.html
Following is the output, when we execute the above code on WINDOWS operating system.
..\file\bose.html
Example
If there is no relative path between from and to paths, the path.relative() method will return the complete to path.
In the below program, we are passing different file paths to from and to parameters of the path.relative() method.
const path = require('path'); var path1 = path.relative('C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', "E:/programs/file") console.log(path1);
Output
After executing the above program in online compiler (POSIX), the path.relative() method returns the complete 'to' path to the output because there is no relative path.
../../../../../../E:/programs/file
Following is the output, when we execute the above code on WINDOWS operating system.
E:\programs\file
Example
The path.relative() method will return a zero-length string if both the from and to paths are zero-length strings.
In the following example, we are passing empty strings to the from and to parameters of the path.relative() method.
const path = require('path'); var path1 = path.relative('', ''); console.log(path1);
Output
If we compile and run the above program, the path.relative() method returns a zero-length string to the output as there are no paths given.
''
Example
If we pass a value that is not a string type to the from and to parameters, the path.relative() method will throw a TypeError.
In the given example, we are passing an instance of an object, instead of a string to both from and to parameters of the method.
const path = require('path'); var path1 = path.relative({}, {}); console.log(path1);
TypeError
As we can see in the output below, the path.relative() method throws a TypeError as the from and to parameters are not a string values.
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.relative (path.js:1172:5) at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:18) 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)