Is there a significant difference when loading files using forward slash vs backward slash in Node.js?

StackOverflow https://stackoverflow.com/questions/8395413

  •  28-10-2019
  •  | 
  •  

Question

I have been writing Node.js apps for quite some time now, and I have been seldom paying any attention to the use of forward slash vs backward slash when loading files using Node.js on Windows.

Now, after using both interchangeably without a problem, I'm starting to wonder, is there a significant difference between the two?

Up until now, the only difference that I have noticed is that JavaScript's string literal (just like C and C++), requires that every backward slash be preceded by another backward slash. (In fact this is why I have been reluctant to use the backward slash, recently -- even for Node.js on Windows --, because I fear forgetting that extra slash.)

Was it helpful?

Solution

Just like what Raynos said in the comments, I should use the forward slash always, even on Windows.

Update

The above answer is really, really outdated. Always use path.join when constructing file paths from scratch.

Example:

// Get file.txt from a folder called `somefolder` that is
// located in the same directory as the currently running
// .js file.
path.join(__dirname, 'somefolder', 'file.txt');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top