0

I know that it is highly recommendable to use path.join if one would like to have his node project Windows compatible.

But do we need to use it also inside require commands? For example, instead of

const colors = require('colors/safe');

to use

const colors = require(path.join('colors', 'safe'));

The question may be a little silly, but I'm a bit lost after searching the require node documentation.

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
  • What programming language are you using? C++? It might be good to add that to the tags. – LuminousNutria Oct 21 '18 at 21:59
  • Why do you think the [answer you already linked](https://stackoverflow.com/questions/9756567/do-you-need-to-use-path-join-in-node-js) is not a duplicate? It appears to already answer the question. Or, put another way, what is not answered for you in that other answer? – jfriend00 Oct 21 '18 at 22:52
  • Forward slash is fine for windows. Just make sure your case is correct. If the case of the file/folder don't match the require, it'll work on windows or Mac, but fail on Linux. – user1751825 Oct 22 '18 at 07:09
  • @jfriend00, the accepted answer raises my concerns and replies to my answer, which was not answered in the other topic. – João Pimentel Ferreira Oct 22 '18 at 09:07
  • @jfriend00, apologies for disturbing, but do you confirm that within `require` the `path.join` is totally superfluous even for Windows OS, since said string within `require` is merely processed by node, and not the OS? Thank you in advance – João Pimentel Ferreira Oct 23 '18 at 18:05
  • 1
    A simple test running `require("./test-subdir/sub.js")` on Windows 10 in node v10.11.0 confirms that it works just fine. I did not delve into the node.js code for `require()` to figure out if it is node.js swapping the forward slashes for backslashes before making a Windows API call to read the file or if it is Windows that is accepting either forward or backslashes. The point is that it works just fine whichever it is. My guess is that both node.js and Windows accept either type of slash as a path delimiter. The Windows command shell does not, but apparently Windows APIs do. – jfriend00 Oct 23 '18 at 21:05

1 Answers1

1

In the require statement the path.join is not necessary because these paths only resolved by node.js. The path.join() method only joins strings together and use the OS specific delemiter. https://nodejs.org/api/path.html#path_path_join_paths

Tip

If you want to pack your node.js application into an executable for example with pkg then it is recommended not using some join statements in require becuse this tool parse some statements to pack the required files into the executable.

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
sweting
  • 390
  • 1
  • 8
  • This is not correct that the paths are only used by node.js, not the OS. To actually test for existence in the file system and to read the files, node.js has to use the OS to access the files and therefore paths are passed to the OS (inside of libuv) at some point in the process. – jfriend00 Oct 23 '18 at 21:09