Home » Node.js

Path Module in Node JS

In this article, we are going to discuss one of the inbuilt modules i.e. PATH MODULE. It provides utilities for working with file and directory paths.
Submitted by Mansha Lamba, on October 18, 2018

Node is essentially a runtime environment for JavaScript programming language.

In C, C++ we have header files; In Java, we have inbuilt classes; Similarly, in Node JS we have inbuilt modules.

These modules are ready to be used by developers and come with a lot of objects and methods that can get complex things done in one command. Hence decreasing coder’s effort by a large amount.

In this article, we are going to discuss one of the inbuilt modules i.e. PATH MODULE. It provides utilities for working with file and directory paths.

Here, I have a sample code for understanding basics of Path module.

Code:

var path=require('path');

console.log(__dirname);
console.log(__filename);

var way=__filename;

console.log(path.normalize(way));
console.log(path.extname(way));
console.log(path.dirname(way));
console.log(path.basename(way));

Output

Node JS | Path Module Output

Explanation of code

For using any inbuilt module we first need to include it in our source code. This is done in line number 1.

require is a keyword and path is the name of the module to be included, it is stored in the variable path (var path). One can even use const instead of var. Also, we could use any other name (on the left side of =) instead of the path but it is a universal convention to use the same name as of module name.

In the last 4 lines you can see different methods of path object being used.

  • EXTNAME - returns extension of the base file.
  • BASENAME - returns name of the base file.
  • DIRNAME - returns whole path of directory or folder in which the base file is present.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.