22

Does the Node.js fs module implicitly convert Windows folder path separators from '\\' to '/'?

For example, if I use this call on Windows:

fs.readdirSync(dir).forEach(function(file) {

});

file argument has '/' path separators, not '\\', why is that?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

2 Answers2

35

Yes it does. See more: Writing cross-platform Node.js

Be sure to use path.join and path.normalize instead of having explicit path separators (/, \, \\, etc) in your code.

marcusshep
  • 1,916
  • 2
  • 18
  • 31
krl
  • 5,087
  • 4
  • 36
  • 53
1

Sometimes. The documentation includes this:

path.win32 The path.win32 property provides access to Windows-specific implementations of the path methods.

The API is accessible via require('node:path').win32 or require('node:path/win32').

The docs also include this:

path.posix The path.posix property provides access to POSIX specific implementations of the path methods.

The API is accessible via require('node:path').posix or require('node:path/posix').

This only really becomes an issue for me when dealing with "drive letters": I try to keep everything relative, but there are times when that is not possible.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63