Path in NodeJS

Path in NodeJS a quick guide

The path module provides utilities for working with file and directory paths.
– NodeJS Documentation

I’ve gone through the NodeJS standard documentation for path and boiled it down to a few less rows using the 80/20 principle which states that about 20% of the effort translates to 80% of the value. By doing this I hope that I’m giving you guys 80% of the value with 20% of the actual reading needed. 🙂

All of the examples was created using Quokka which I wrote about in my previous post about VSCode Extensions.

join() and resolve()

1
2
3
4
5
6
// Concatenates the subpaths (removes '/' when needed)
path.join('/foo', '/bar', 'baz', 'index.js'); '/foo/bar/baz/index.js'

// Joins all sequences until absolute path is found, examples below skips /foo
// since we'we got our absolute path at '/bar' already.
path.resolve('/foo', '/bar', 'baz', 'index.js'); 'bar/baz/index.js'

basename()

1
2
3
4
// Returns the last portion of the path.
path.basename('foo/bar/baz.js'); 'baz.js'
// Returns the last portion, removing the extension (second argument)
path.basename('foo/bar/baz.js', '.js'); 'baz'

dirname()

1
2
// Returns the directory name of a path.
path.dirname('foo/bar/baz/file.js'); 'foo/bar/baz'

extname()

1
2
3
4
// Returns the last extension (after last '.' in path)
path.extname('foo/bar/baz/file.js'); '.js'
path.extname('foo/bar/baz/file.js.gz'); '.gz'
path.extname('foo/bar/baz'); [empty string]

format() and parse()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Helper to create a path string from it's parts
path.format({
    dir: 'foo/bar', // Priority over root
    root: '/',
    base: 'baz', // Priority over name and ext
    name: 'index',
    ext: '.js'
}); 'foo/bar/baz'

console.log(path.format({
    //dir: 'foo/bar', // Priority over root
    root: '/',
    //base: 'baz', // Priority over name and ext
    name: 'index',
    ext: '.js'
})); '/index.js'


// Reverse of path.format()
path.parse('foo/bar/baz/index.js'); { root: '', dir: 'foo/bar/baz', base: 'index.js', ext: '.js', name: 'index }

delimiter

1
2
3
4
5
6
// Gets the current systems delimiter for PATH. ( : for POSIX ; for Windows )
path.delimiter; ':'

// Can be used for splitting paths.
process.env.PATH; 'oracle/bin:/usr/lib/jvm:/usr/local/bin'
process.env.PATH.split(path.delimiter); ['/oracle/bin', '/usr/lib/jvm', '/usr/local/bin']

isAbsolute()

1
2
3
4
5
// Check if a path is absolute (starting with '/')
path.isAbsolute('/foo/bar'); 'true'
path.isAbsolute('/bar'); 'true'
path.isAbsolute('foo/bar'); 'false'
path.isAbsolute('.'); 'false'

normalize()

1
2
// Resolves '..' and '.' segments in the path string
path.normalize('foo/bar//baz/../index.js')

relative()

1
2
3
// The relative path from the first path to the next (from/to).
path.relative('foo/bar', 'foo/bar/baz/index.js') 'baz/index.js'
path.relative('foo/bar/index.js', 'foo/bar/index.js') [empty string]

sep

1
2
// The current systems path segmenet seperator
path.sep '/'

That’s it!

That’s my summary of NodeJS path. What did you think about this post? too little information or just enough? I’m trying to reduce the amount of ‘extra’ text in my posts to get to the stuff that gives the most back to you a reader.

This post was reduce by ~80% in text from it’s starting point to try to use the 80/20 principle. It states that it’s about 20% of the effort put into something that gives 80% of the value. I’m hoping I just gave you as readers 80% of the value with 20% of the text! 🙂

Please follow and like SnappyJS:

3 thoughts on “Path in NodeJS a quick guide

  1. I think this is one of the most significant info for me.
    And i am glad reading your article. But wanna
    remark on few general things, The site style is great,
    the articles is really great : D. Good job,
    cheers

Leave a Reply

Your email address will not be published.