Last Updated: February 25, 2016
·
1.916K
· DamonOehlman

Let `path.join` help out with platform specific separators

When writing your Node programs, it's well worth using path.join rather than simply concatenating path strings yourself, for example:

var path = require('path'),
    testPath = path.join('a', 'b', 'c');

Is a better way to go than simply defining testPath as a/b/c. While I don't use Windows very much (if at all), I've noticed that libraries where I use path.join more strictly port across to work well on Windows whereas I get path separator compatibility problems if not.

This technique can also be applied when using path.resolve and resolving against base path. Simply provide more than two args, and you'll get back a path joined with the appropriate path.sep for your current platform. e.g.

var path = require('path'),
    testPath = path.resolve(__dirname, 'a', 'b', 'c');

Well worth keeping in mind, I reckon, given there's quite a few people using Node in Windows land now too :)