I came home with a work project that I planned on fiddling with on my personal computer, I installed everything, using the exact same environment (Node v0.11.12), etc. Start the project, then I'm greeted with messages complaining that the config loader module cannot locate a file (that exists and is at the path exposed by the error).
Looking closer at the error, I realize that the problem is path.join()
. Where
path.join('./foo/bar');
// 'foo/bar'
Which is not good. Why does path.join
remove the leading period?
The above is simply an example. The program make use of the function like
var configFile = require(path.join(modulePath, 'conf', file));
for example, where modulePath
is relative to the current working directory (i.e. ./app/module/
)
Node.js | path.join () Method Last Updated : 14 Feb, 2020 The path.join () method is used to join a number of path-segments using the platform-specific delimiter to form a single path. The final path is normalized after the joining takes place.
The join () method from the path module in NodeJS provides you with a way to concatenate multiple path segments together to form a new path.
Node.js | path.resolve () Method Last Updated : 14 Feb, 2020 The path.resolve () method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created.
The join () method from the path module in NodeJS provides you with a way to concatenate multiple path segments together to form a new path. path.join(path, ...)
This is correct behavior, and is documented in the Path.join documentation:
Join all arguments together and normalize the resulting path.
It is correct because foo/bar
is the normalized (canoncial) form of ./foo/bar
, just as it is the normalized form of ./foo/././bar/.
or foo/baz/../bar
.
(Differences between require('./foo/bar')
and require('foo/bar')
, and any resulting problems of such, should be specifically addressed in a different question without path.join
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With