Search code examples
node.jsrequire

node - Do we need to use path.join() inside require() for Windows compatibility?


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.


Solution

  • 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.