Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend

Path Module

The path module is the all-inclusive module for working with file paths in Node. Each operating system uses a different file path separator for identifying and/or creating file paths.

For example, Windows uses the backslash; UNIX-flavored systems use the forward slash. Window’s versions of Node can handle forward slashes effectively, but most Windows applications cannot.

To resolve the cross-platform path separator problem, Node provides the sep property of the path module. Use sep when building directory paths because it automatically determines what is needed based on the OS. An example appears below:

var path = require("path");
var directories = ["dirA", "dirB", "dirC"];
var directory = directories.join(path.sep);

console.log(directory);

After executing the above code, you should see dirA\dirB\dirC on Windows or dirA/dirB/dirC on UNIX. Similarly, Windows places a semicolon to separate paths in a path environment variable. Other systems use a colon.

Node provides the delimiter property to fix this:

var path = require("path");

process.env.PATH.split(path.delimiter).forEach(function(dir) {
  console.log(dir);
});

The code above correctly parses out the paths for your path variable, no matter which operating system you’re running it from.

Probably the most useful methods are those path methods that parse the actual path into extensions, file name, and directory. For example, do the following to get the file extension:

var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var extension = path.extname(fileName);

console.log(extension);

To get the file name portion of the file name, the basename method is used:

var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var file = path.basename(fileName);

console.log(file);

If you want the file name without the extension, you can pass the extension variable (containing the extension name) to the basename method telling Node to return only the name without the extension:

var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var extension = path.extname(fileName);
var file = path.basename(fileName,extension);

console.log(file);

To return only the directory portion of the path, use the dirname method:

var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var dirName = path.dirname(fileName);

console.log(dirName);

Sometimes your application may handle paths that are not normalized (containing multiple periods, etc.). Node provides the path.normalize() method to correct this:

var path = require("path");
var dirName = "\\DirA\\DirB\\..\\..\\DirC";
var normalized = path.normalize(dirName);

console.log(normalized);

A really handy method for determining the relative path between directories is called relative(), which takes two directory names as parameters and determines the relative path between the two directories:

var path = require("path");
var dirName1 = "C:\\Python27\\ArcGIS10.2\\Scripts";
var dirName2 = "C:\\Program Files (x86)\\AlcorMicroData\\CommonAppData";
var relativePath = path.relative(dirName1, dirName2);

console.log(relativePath);

The output is:  ..\..\..\Program Files (x86)\AlcorMicroData\CommonAppData
  • + Share This
  • 🔖 Save To Your Account