
- 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.normalize() Method
The Node.js path.normalize() method of the path module normalizes the given path. If any segment of the path contains strings like '..' and '.', this method will eliminate them and provides a complete normalized path.
If any multiple sequential path segment separators are present in the given path, they will be replaced by a single instance of the platform-specific path segment seperator ('\' on windows and '/' on posix). This method can also be used to resolve relative paths, making it easier to construct valid paths regardless of the current working directory. In this method, the trailing separators are preserved.
Syntax
Following is the syntax of the Node.js path.normalize() method of path module −
path.normalize( path )
Parameters
path − This parameter specifies the path to be normalized.
Return value
This method will return a string that specifies the normalized path.
Example
If a path is passed to the method, it will normalize the git.
In the following example, we trying to perform the usage of the method by proving it a path.
const path = require('path'); var result = path.normalize("/Users/Lenovo/Desktop/JavaScript/Nodefile.js"); console.log(result);
Output
After executing the above program in online compiler (POSIX), the path.normalize() method returned the normalized path.
/Users/Lenovo/Desktop/JavaScript/Nodefile.js
Following is the output, when we execute the above code on WINDOWS operating system.
\Users\Lenovo\Desktop\JavaScript\Nodefile.js
Example
If '..' is passed as a segment of the path, this method will rectify it and normalize the path.
In the given program, we are providing (..) as a path segment to the path.
const path = require('path'); var result = path.normalize("/Users/Lenovo/Desktop/../Nodefile.js"); console.log(result);
Output
After executing the above program in online compiler (POSIX), this method eliminated the segments that contain '..' and returned a normalized path.
/Users/Lenovo/Nodefile.js
Following is the output when we execute the above code on WINDOWS operating system.
\Users\Lenovo\Nodefile.js
Example
If '.' is passed as a segment of the path, the path.normalize() method will eliminate it and normalize the path.
In the example below, we are providing (.) as a path segment to the path.
const path = require('path'); var result = path.normalize("/Users/Lenovo/./JavaScript/Nodefile.js"); console.log(result);
Output
After executing the above program in the online compiler (POSIX), the method returns the complete normalized path by eliminating ".".
/Users/Lenovo/JavaScript/Nodefile.js
Following is the output, when we execute the above code on WINDOWS operating system.
\Users\Lenovo\JavaScript\Nodefile.js
Example
If the WINDOWS or POSIX recognize multiple path separators in the path, then they will be replaced by the instances of the preferred separator ('\' on windows and '/' on posix).
In the given program below, we are providing multiple path providers to the path.
const path = require('path'); var result = path.normalize("/Users/////Lenovo/////Desktop/////Nodefile.js"); console.log(result);
Output
If we compile and run the above program in online compiler (POSIX), the path.normalize() method returns the normalized string path by replacing multiple path separators with a single separator.
/Users/Lenovo/Desktop/Nodefile.js
Following is the output, when we execute the above code on WINDOWS operating system.
\Users\Lenovo\Nodefile.js
Example
If we pass a value that is not a string type to the path parameter, the method will throw a TypeError.
In the given example, we are passing an array instead of a string to the path parameter of the method.
const path = require('path'); var result = path.normalize([]); console.log(result);
TypeError
As we can see in the output below, the 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.normalize (path.js:1122: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)