- 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.join() Method
The Node.js path.join() method of the path module will accept a series of string path segments. This method will join all the given string path segments together by using the platform separator, then it normalizes the complete path.
The Path segments of zero length are ignored. The '.' will be printed if the joined path string has zero length. A TypeError will be thrown if any of the path segments is not a string value.
Syntax
Following is the syntax of the Node.js path.join() method of path module −
path.join([...paths])
Parameters
...paths − This parameter holds a sequence of path segments that are separated by a comma.
Return value
This method returns a complete string path by joining all the string path segments which are given in the ...paths parameter.
The below examples demonstrate the usage of the path.join() method of the path module.
Example
The path.join() method takes a series of string path segments as input and outputs a complete string path.
In the example below, we are passing a sequence of string path segments to the path.join() method.
const path = require('path'); var result = path.join('Users', 'Node.js', 'API', 'Path module'); console.log(result);
Output
If we compile and run the above program, this method joins all the given path segments together by the platform-specific separator as a delimiter and prints the complete path.
Users/Node.js/API/Path module
Example
The path.join() method accepts a sequence of string path segments and returns them as a complete string path.
In the following example, we are trying to join a string path segment ("Nodefile.js") with the current working directory. We can get the current working directory by using '__dirname' property.
const path = require('path'); var result = path.join(__dirname, "Nodefile.js"); console.log('Current working directory: ' + __dirname); console.log("After the joining a segment: " + result);
Output
After executing the above program in online compiler (POSIX), this method joins the string path segment ("Nodefile.js") with the current working directory.
Current working directory: /home/cg/root/63a028ab0650d After the joining a segment: /home/cg/root/63a028ab0650d/Nodefile.js
Following is the output, when we execute the above code on windows operating system.
Current working directory: C:\Users\Lenovo\Desktop\JavaScript After the joining a segment: C:\Users\Lenovo\Desktop\JavaScript\Nodefile.js
Example
If we pass a value that is not a string type to the …paths parameter, the method will throw a TypeError.
In the following example, we are passing an object instead of a string to the …paths parameter of the path.isAbsolute() method.
const path = require('path'); var result = path.join({}); console.log( result);
Output
As we can see in the output below, the path.isAbsolute() method throws a TypeError as the …paths 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.join (path.js:1157:7) at Object.<anonymous> (/home/cg/root/63a028ab0650d/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)
To Continue Learning Please Login
Login with Google