Easy Tutorial
❮ Nodejs Util Nodejs Mongodb ❯

Node.js Path Module

Node.js Utility Module

The Node.js path module provides utilities for working with file and directory paths. You can include this module using the following:

var path = require("path")

Methods

No. Method & Description
1 path.normalize(p) <br>Normalizes the path, taking care of '..' and '.'.
2 path.join([path1][, path2][, ...]) <br>Joins all given path segments together using the platform-specific separator as a delimiter.
3 path.resolve([from ...], to) <br>Resolves the 'to' parameter into an absolute path. The sequence of paths is processed from right to left, with each subsequent path prepended until an absolute path is constructed. For example, given the sequence: /foo, /bar, baz, calling path.resolve('/foo', '/bar', 'baz') would return /bar/baz. path.resolve('/foo/bar', './baz');<br>// Returns: '/foo/bar/baz'<br>path.resolve('/foo/bar', '/tmp/file/');<br>// Returns: '/tmp/file'<br>path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');<br>// If the current working directory is /home/myself/node,<br>// it returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
4 path.isAbsolute(path) <br>Determines if the path is an absolute path.
5 path.relative(from, to) <br>Returns the relative path from 'from' to 'to' based on the current working directory. On Linux: path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');<br>// Returns: '../../impl/bbb' On Windows: path.relative('C:\orandea\test\aaa', 'C:\orandea\impl\bbb');<br>// Returns: '..\..\impl\bbb'
6 path.dirname(p) <br>Returns the directory name of a path, similar to the Unix dirname command.
7 path.basename(p[, ext]) <br>Returns the last portion of a path, similar to the Unix basename command.
8 path.extname(p) <br>Returns the extension of the path, i.e., the portion following the last '.' in the path. If there is no '.' in the path or the first character is '.', it returns an empty string.
9 path.parse(pathString) <br>Returns an object from the path string.
10 path.format(pathObject) <br>Returns a path string from an object, the opposite of path.parse.

Properties

No. Property & Description
1 path.sep <br>The platform-specific file separator, '\' or '/'.
2 path.delimiter <br>The platform-specific path delimiter, ';' or ':'.
3 path.posix <br>Provides access to the aforementioned path methods but always interacts in a posix-compatible way.
4 path.win32 <br>Provides access to the aforementioned path methods but always interacts in a win32-compatible way.

Example

Create a main.js file with the following code:

var path = require("path");

// Normalize a path
console.log('normalization : ' + path.normalize('/test/test1//2slashes/1slash/tab/..'));

// Join multiple paths together
console.log('joint path : ' + path.join('/test', 'test1', '2slashes/1slash', 'tab', '..'));
// Convert to absolute path
console.log('resolve : ' + path.resolve('main.js'));

// File extension in the path
console.log('ext name : ' + path.extname('main.js'));

Code execution results are as follows:

$ node main.js 
normalization : /test/test1/2slashes/1slash
joint path : /test/test1/2slashes/1slash
resolve : /web/com/1427176256_27423/main.js
ext name : .js

Node.js Utility Modules

❮ Nodejs Util Nodejs Mongodb ❯