Node js modules simplified: The Path module.

siradji

Suraj Auwal

Posted on January 31, 2021

Node js modules simplified: The Path module.

The path module is one of the most used at the same time misunderstood modules in node js. Node js has a bunch of modules that ease our lives as developers.

What is the Path module in node?

From the node js docs, The path module provides utilities for working with file and directory paths. It can be accessed using. Yes, a module that will ease your life when working with files and directories.

Like any other modules in Node, the Path module has quite a several methods. Today, I will be discussing the five methods of the path module.

1. Path.join()

This method, in my opinion, is one if not the most used method of the path module. It is sometimes confused by beginners with the resolve method.

The join method joins (as its name says) all the path chunks passed and returns a path string. so something like

const path = require('path')

const dir = path.join('home', 'work', 'my-project');

console.log(dir);
//
home/work/my-project'
Enter fullscreen mode Exit fullscreen mode

I know you might be wondering why to use the path method when you can just type out the path. Well, the join method does just more than joining path segments. It joins the segments with a platform-specific separator as a delimiter, then normalizes the resulting path. The delimiter of a macOS is different from that of windows. The join module joins these path segments with a platform-specific delimiter.

That is good. Imagine you work in a team of many developers and some of them are using Mac while you are on windows. Specifying a path explicitly will result in your program breaking on your team members' computers.

2. Path.resolve()

This is an important path method that this article will be incomplete if missing. This method is also confused with the join method. True, both of them return a path, but the resolve method resolves the path segments that are passed as arguments and returns an absolute URL.

Confusing? Let take a look at what resolve method will return passing the same argument we had above.

const path = require('path')

const dir = path.join('home', 'work', 'my-project');

console.log(dir);
//
'/home/kraken/projects/personal/community/dev.to/nest_one/home/work/my-project'

Enter fullscreen mode Exit fullscreen mode

As you can see, the path returned now is an absolute URL using your home directory as the base. I use this most of the time if I need to point to a file for example a png file.

3.Path.extname()

Honestly, the names of these methods are quite self-explanatory. At a glance, you know that this method has something to do with the extension name. If you think so, then you are right. This method returns the extension of a given file. Let us take a look

const path = require('path');

const fileExtension = path.extname('/foo/bar/node.js');
console.log(fileExtension);
//
'.js'

Enter fullscreen mode Exit fullscreen mode

Keep in mind, this method returns the extension of the path, from the last occurrence of the. (period) character to the end of the string in the last portion of the path.

const path = require('path');

const fileExtension = path.extname('/foo/bar/node.js.md');
console.log(fileExtension);
//
'.md'

Enter fullscreen mode Exit fullscreen mode

I recently used this method to filter out images that a program I was writing does not support. A user uploads a png file and my program (as per the client's instruction) wants only a jpeg file. You can see how easy it is for me to do that.

4.Path.isAbsolute()

This method takes a string path as an argument and returns a boolean. It is the only path method that returns a boolean.
This method is used to check if a given path (passed as an argument) is an absolute path.

What is an absolute path?
An absolute path always contains the root element and the complete directory list required to locate the file.

const path = require('path');

const isValidPath = path.isAbsolute('/foo/bar/node');
// true
const path = require('path');

const fileExtension = path.extname('myfile.pdf');
//false

Enter fullscreen mode Exit fullscreen mode

if you want to validate a path, you can use this method to do so.

5. Path.parse()

Last but not least, the parse method. This method is, in my opinion. very cool. The parse method accepts a path as an argument and returns an object with the "information" about the path. The 'information' returned as dir, ext, name, base, root. It is very helpful when you want to extract information about a path.

const path = require('path');

const pathProps = path.parse('/foo/bar/node.js');

//
{
  root: '/',
  dir: '/foo/bar',
  base: 'node.js',
  ext: '.js',
  name: 'node'
}
Enter fullscreen mode Exit fullscreen mode

That is it, guys! I hope you have learned something out of this. I would write follow-up articles on other modules like the Event, FS, and OS modules.

Cheers and happy coding!

💖 💪 🙅 🚩
siradji
Suraj Auwal

Posted on January 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related