如何定位父文件夹?

时间:2022-11-20 07:25:59

How do I write this to go back up the parent 2 levels to find a file?

我怎么写这个回到父2级找到一个文件?

fs.readFile(__dirname + 'foo.bar');

8 个解决方案

#1


169  

Try this:

试试这个:

fs.readFile(__dirname + '/../../foo.bar');

Note the forward slash at the beginning of the relative path.

注意相对路径开头的斜杠。

#2


113  

Use path.join http://nodejs.org/docs/v0.4.10/api/path.html#path.join

使用路径。加入http://nodejs.org/docs/v0.4.10/api/path.html path.join

var path = require("path"),
    fs = require("fs");

fs.readFile(path.join(__dirname, '../..', 'foo.bar'));

path.join() will handle leading/trailing slashes for you and just do the right thing and you don't have to try to remember when trailing slashes exist and when they dont.

join()将为您处理引导/拖尾斜线,并只做正确的事情,您不必试图记住拖尾斜线何时存在以及何时不存在。

#3


58  

I know it is a bit picky, but all the answers so far are not quite right.

我知道这有点挑剔,但到目前为止所有的答案都不太对。

The point of path.join() is to eliminate the need for the caller to know which directory separator to use (making code platform agnostic).

join()的目的是消除调用者知道使用哪个目录分隔符(使代码平台不可知)的需要。

Technically the correct answer would be something like:

从技术上来说,正确的答案应该是:

var path = require("path");

fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));

I would have added this as a comment to Alex Wayne's answer but not enough rep yet!

我本想把这句话加到阿历克斯·韦恩的回答中,但还没有足够的代表!

EDIT: as per user1767586's observation

编辑:根据user1767586的观察

#4


42  

The easiest way would be to use path.resolve:

最简单的方法是使用路径。

path.resolve(__dirname, '..', '..');

#5


9  

Looks like you'll need the path module. (path.normalize in particular)

看起来您需要路径模块。(路径。特别是规范化)

var path = require("path"),
    fs = require("fs");

fs.readFile(path.normalize(__dirname + "/../../foo.bar"));

#6


8  

If another module calls yours and you'd still like to know the location of the main file being run you can use a modification of @Jason's code:

如果另一个模块调用了您的,并且您仍然想知道正在运行的主文件的位置,您可以使用@Jason的代码修改:

var path = require('path'),
    __parentDir = path.dirname(process.mainModule.filename);

fs.readFile(__parentDir + '/foo.bar');

That way you'll get the location of the script actually being run.

通过这种方式,您将获得实际运行的脚本的位置。

#7


6  

If you not positive on where the parent is, this will get you the path;

如果你对亲本的位置不确定,这就会给你指明方向;

var path = require('path'),
    __parentDir = path.dirname(module.parent.filename);

fs.readFile(__parentDir + '/foo.bar');

#8


4  

You can use

您可以使用

path.join(__dirname, '../..');

#1


169  

Try this:

试试这个:

fs.readFile(__dirname + '/../../foo.bar');

Note the forward slash at the beginning of the relative path.

注意相对路径开头的斜杠。

#2


113  

Use path.join http://nodejs.org/docs/v0.4.10/api/path.html#path.join

使用路径。加入http://nodejs.org/docs/v0.4.10/api/path.html path.join

var path = require("path"),
    fs = require("fs");

fs.readFile(path.join(__dirname, '../..', 'foo.bar'));

path.join() will handle leading/trailing slashes for you and just do the right thing and you don't have to try to remember when trailing slashes exist and when they dont.

join()将为您处理引导/拖尾斜线,并只做正确的事情,您不必试图记住拖尾斜线何时存在以及何时不存在。

#3


58  

I know it is a bit picky, but all the answers so far are not quite right.

我知道这有点挑剔,但到目前为止所有的答案都不太对。

The point of path.join() is to eliminate the need for the caller to know which directory separator to use (making code platform agnostic).

join()的目的是消除调用者知道使用哪个目录分隔符(使代码平台不可知)的需要。

Technically the correct answer would be something like:

从技术上来说,正确的答案应该是:

var path = require("path");

fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));

I would have added this as a comment to Alex Wayne's answer but not enough rep yet!

我本想把这句话加到阿历克斯·韦恩的回答中,但还没有足够的代表!

EDIT: as per user1767586's observation

编辑:根据user1767586的观察

#4


42  

The easiest way would be to use path.resolve:

最简单的方法是使用路径。

path.resolve(__dirname, '..', '..');

#5


9  

Looks like you'll need the path module. (path.normalize in particular)

看起来您需要路径模块。(路径。特别是规范化)

var path = require("path"),
    fs = require("fs");

fs.readFile(path.normalize(__dirname + "/../../foo.bar"));

#6


8  

If another module calls yours and you'd still like to know the location of the main file being run you can use a modification of @Jason's code:

如果另一个模块调用了您的,并且您仍然想知道正在运行的主文件的位置,您可以使用@Jason的代码修改:

var path = require('path'),
    __parentDir = path.dirname(process.mainModule.filename);

fs.readFile(__parentDir + '/foo.bar');

That way you'll get the location of the script actually being run.

通过这种方式,您将获得实际运行的脚本的位置。

#7


6  

If you not positive on where the parent is, this will get you the path;

如果你对亲本的位置不确定,这就会给你指明方向;

var path = require('path'),
    __parentDir = path.dirname(module.parent.filename);

fs.readFile(__parentDir + '/foo.bar');

#8


4  

You can use

您可以使用

path.join(__dirname, '../..');