Node.js Path Module?

TheDevStory
2 min readSep 12, 2024

Since the birth of computers, file paths have been crucial for developers. In early days of programming, particularly with language like C, developers had to craft their own functions to handle file paths and locations. Like below.

#include <stdio.h>
#include <string.h>

void get_filename(const char *path, char *filename) {
const char *last_slash = strrchr(path, '/');
if (last_slash == NULL) {
strcpy(filename, path);
} else {
strcpy(filename, last_slash + 1);
}
}

int main() {
char path[] = "/home/user/documents/file.txt";
char filename[256];

get_filename(path, filename);
printf("Filename: %s\n", filename);

return 0;
}

Like this even a simple task like extracting a file name requires tedious work. However modern programming languages provide these functions by default. And Node.js has a path module to take charge of these operations.

6 basic functions of Path modules that make our javascript coding easier.

  1. path.join() : Joins all given path segments together using the separator.
  2. path.resolve() : Resolves a sequence of path or path segments into an absolute path.
  3. path.basename() : Returns the last portion of a path, typically a file name.
  4. path.dirname() : Returns the directory name of a path.
  5. path.extname() : Returns the extension of the path, from the last occurrence of th ‘.’ character th the end of the string.
  6. path.parse() : Returns an object whose properties represent significant elements of the path.

These are the basic functions of the Path modules. Now, I will show you some example of how to use them.

6 basic functions code example

const path = require('path');

// 1. path.join()
console.log(path.join('/home', 'user', 'documents', 'file.txt'));
// Output : /home/user/documents/file.txt

// 2. path.resolve()
console.log(path.resolve('folder', 'subfolder', 'file.txt'));
// Output : /current/working/directory/folder/subfolder/file.txt

// 3. path.basename()
console.log(path.basename('/home/user/documents/file.txt'));
// Output : file.txt

// 4. path.dirname()
console.log(path.dirname('/home/user/documents/file.txt'));
// Output : /home/user/documents

// 5. path.extname()
console.log(path.extname('/home/user/documents/file.txt'));
// Output : .txt

// 6. path.parse()
console.log(path.parse('/home/user/documents/file.txt'));
// Output : { root: '/', dir: '/home/user/documents', base: 'file.txt', ext: '.txt', name: 'file' }

// 7. path.normalize()
console.log(path.normalize('/home/user/../documents/./file.txt'));
// Output : /home/documents/file.txt

As you can see, we can make our javascript work much easier with Path module. While the examples I’ve shown you are typically used for serious and often mundane tasks, we can sometimes play around with this module in creative ways. Here’s a fun example

const path = require('path');

function createMessage() {
const heart = path.join('(', '<3', ')');
const star = path.join('(', '*', ')');
const smile = path.join('(', '^_^', ')');

const message = [
path.join('You', 'are', 'awesome', heart),
path.join('Keep', 'shining', star),
path.join('Stay', 'positive', smile),
path.join('You', 'can', 'do', 'it', '!')
];

return message.join('\n');
}

console.log(createLovelyMessage());


/*
* You/are/awesome/(<3)
* Keep/shining/(*)
* Stay/positive/(^_^)
* You/can/do/it/!
*/

console.log(path.join('thank', 'you', 'for', 'reading!'));

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

TheDevStory
TheDevStory

Written by TheDevStory

A web developer crafting online experiences. Also football(soccer) coach and Spanish Learner.

No responses yet

Write a response