Node path.relative returns invalid path

I'm sure I should be wrong, but in Node, path.relative seems to display the wrong directory, or at least I did not expect:

> path.relative('a/file.js', 'a/file.css') > '../file.css' 

However, I expect the result to be something like this:

 > './file.css' 

In essence, I am trying to calculate the difference in two ways, so that one file is require other, and ../file.css is obviously wrong for my requirement, since both files are in the a directory. The result shows that file.css is in the parent directory.

What am I missing?

+6
source share
1 answer

As far as I can tell, path.relative() expects as the first argument to a folder, not a file. It works:

 path.relative('a', 'a/file.css') > 'file.css' 

Here is the source code for path.relative : https://github.com/joyent/node/blob/master/lib/path.js#L504-L530 https://github.com/joyent/node/blob/master/lib /path.js#L265-L304

(Note: if you change line numbers in the future, here is a link to the source, as it was when I was writing this: <a2> .)

As you can see, the paths are divided into slashes in them, and it just compares the number of parts, so it does not work if you pass the file instead of the folder as the from argument.

This may be a mistake, or at least should be better documented. You can submit a bug report here: https://github.com/joyent/node/issues/new

+4
source

Source: https://habr.com/ru/post/989670/


All Articles