Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js path.join removes leading period

Tags:

node.js

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?

** Note **

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/)

like image 619
Yanick Rochon Avatar asked Apr 16 '14 00:04

Yanick Rochon


People also ask

What is path join in Node JS?

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.

What is the use of Join () method in NodeJS?

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.

How do I resolve a node node 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.

How to concatenate multiple path segments together in NodeJS?

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, ...)


1 Answers

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

like image 165
user2864740 Avatar answered Oct 23 '22 10:10

user2864740