Node.js Path Module?

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.
path.join()
: Joins all given path segments together using the separator.path.resolve()
: Resolves a sequence of path or path segments into an absolute path.path.basename()
: Returns the last portion of a path, typically a file name.path.dirname()
: Returns the directory name of a path.path.extname()
: Returns the extension of the path, from the last occurrence of th ‘.’ character th the end of the string.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!'));