Node.js Path
The Node.js Path module plays an essential role in handling and transforming file paths efficiently. Whether you’re working on a server-side project or building a CLI tool, managing file and directory paths correctly is crucial. The Path module simplifies these tasks with a set of powerful methods that help you work seamlessly across different operating systems.
Introduction to Applied AI:–Click Here
Importing the Path Module
You can import the path module in Node.js using the following syntax:
var path = require("path");
Node.js Path Methods
Below is a list of commonly used methods available in the Path module:
Data Science Tutorial:-Click Here
| Index | Method | Description |
|---|---|---|
| 1 | path.normalize(p) | Normalizes a string path by resolving .. and . parts. |
| 2 | path.join([path1][, path2][, …]) | Joins all given path segments together and normalizes the resulting path. |
| 3 | path.resolve([from …], to) | Resolves an absolute path from a sequence of paths or path segments. |
| 4 | path.isAbsolute(path) | Determines whether a given path is absolute. Absolute paths always resolve to the same location regardless of the working directory. |
| 5 | path.relative(from, to) | Returns the relative path from one location (from) to another (to). |
| 6 | path.dirname(p) | Returns the directory name of a path, similar to the Unix dirname command. |
| 7 | path.basename(p[, ext]) | Returns the last portion of a path, similar to the Unix basename command. |
| 8 | path.extname(p) | Returns the extension of a path, from the last . to the end of the string. If no extension is found, it returns an empty string. |
| 9 | path.parse(pathstring) | Returns an object containing properties such as root, dir, base, ext, and name from a given path string. |
| 10 | path.format(pathobject) | Constructs a path string from an object—essentially the opposite of path.parse(). |
Node.js Path Example
File: path_example.js
Download New Real Time Projects :–Click here
var path = require("path");
// Normalization
console.log('Normalization: ' + path.normalize('/updategadh/projects//node/newfolder/tab/..'));
// Join
console.log('Joined Path: ' + path.join('/updategadh', 'projects', 'node/newfolder', 'tab', '..'));
// Resolve
console.log('Resolved Path: ' + path.resolve('path_example.js'));
// Extension
console.log('Extension Name: ' + path.extname('path_example.js'));
Running the Example
Open your Node.js command prompt and run the file using:
node path_example.js
Output Example:
Machine Learning Tutorial:–Click Here
Normalization: /updategadh/projects/node/newfolder
Joined Path: /updategadh/projects/node/newfolder
Resolved Path: /Users/username/path_example.js
Extension Name: .js
Complete Advance AI topics:-Â CLICK HERE
Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here
node js path windows
node.js path environment variable
node js path module
node js path.join __dirname
npm path
node js __dirname
path.join in node js
how to set node js path in environment variable in windows
node js path example
node js path in javascript
node js path w3schools