0

In node.js path module, I have this

var path = require('path');
var filepath = path.join("C:/hello", "/foo/bar");
console.log(filepath);

but it prints out

C:\hello\foo\bar

I expected it to use the slashes direction from the first parameter, which is / but it used \. Does anyone how can I can fix this?

If I use the one with \ slashes, and then read from the file using fs module, I get

{ Error: EISDIR: illegal operation on a directory, open 'C:\main\temp\config
1\folder\plugin\jquery-3.1.1.min.js'
    at Error (native)
  errno: -4068,
  code: 'EISDIR',
  syscall: 'open',
  path: 'C:\\main\\temp\\config1\\folder\\plugin\\jquery-3.1.1.min.js' }

I'm running on a windows environment btw.

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474

1 Answers1

0

About the question why it turns your forward slashes into backslashes, from the Node.js Path module documentation

The path.join() method join all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.

So, as you run on Windows, it will always use back slashes (and seemingly convert any other type of delimiter to them) when you join using the Path module.

You can fix this by...not using path.join and simply building the path yourself, which is advised against if you really want to go with being capable of using the utility you write on multiple platforms.


About your second addition, another quote:

EISDIR means that the target of the operation is a directory in reality but that the expected filetype of the target is something other than a directory.

Found in this StackOverflow thread.

Community
  • 1
  • 1
pixeldesu
  • 632
  • 6
  • 12