Windows 路径上的 path.dirname 为“.”

Bla*_*goh 5 node.js electron

我在电子领域正在做:

 path.dirname('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main')
Run Code Online (Sandbox Code Playgroud)

这条路径就是我的实际价值__dirname。为什么它不给我 C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron?我想把那main部分砍掉。

Vis*_*hal 0

假设main是一个directory里面electron。还假设您有一些名为index.jsinside 的文件main,您希望将path of electron directory.

所以,你可以这样做 path.join :

var mainFolderParentPath = path.join(__dirname, '../');
Run Code Online (Sandbox Code Playgroud)

您的原始文件位置:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main\\index.js
Run Code Online (Sandbox Code Playgroud)

__dirname 将返回

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main
Run Code Online (Sandbox Code Playgroud)

然后在 path.join '../' 内,将从路径中切掉主文件夹。所以,你会留下:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron
Run Code Online (Sandbox Code Playgroud)