path.join in nodejs

path.join() does two things :

  • Joins all given path seg­ments together using the plat­form spe­cific sep­a­ra­tor as a delimiter
  • Nor­mal­izes the result­ing path
path.join('src/', 'js/Router.jsx');

'src/js/Router.jsx' on OSX and Linux

'srcjsRouter.jsx' on Windows

Here, it nor­mal­izes the path by remov­ing extra /.


Rec­om­mended way to use path.join to make it work across platform.

path.join(__dirname, 'src', 'js', 'Router.jsx');

Note that we are just spec­i­fy­ing the folder (or direc­tory) name. And let path.join add OS spe­cific separators.



You may also like...