• Node.js Video Tutorials

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)
nodejs_path_module.htm
Advertisements