node.js 中的 __dirname 和 ./有什么区别? [英] What is the difference between __dirname and ./ in node.js?
问题描述
在 Node.js 中编程并引用与当前目录相关的某个位置的文件时,是否有任何理由使用 __dirname
变量而不是常规的 ./代码>?到目前为止,我一直在我的代码中使用 ./并且刚刚发现
__dirname
的存在,并且基本上想知道将我的 ./转换为它是否明智,如果是这样,为什么这是一个聪明的主意.
要点
在 Node.js 中,__dirname
始终是当前正在执行的脚本所在的目录 (见这个).因此,如果您在 /d1/d2/myscript.js
中键入 __dirname
,则该值将是 /d1/d2
.
相比之下,当您使用像 这样的库时,
和 .
为您提供在终端窗口(即您的工作目录)中运行 node
命令的目录路径fs
.从技术上讲,它最初是您的工作目录,但可以使用 process.chdir()
进行更改.
例外情况是当您将 .
与 require()
一起使用时.require
中的路径总是相对于包含对 require
调用的文件.
例如...
假设您的目录结构是
/dir1/目录2路径测试.js
和pathtest.js
包含
var path = require("path");console.log(". = %s", path.resolve("."));console.log("__dirname = %s", path.resolve(__dirname));
你会
cd/dir1/dir2节点路径测试.js
你得到
<预><代码>.=/目录1/目录2__dirname =/dir1/dir2您的工作目录是 /dir1/dir2
所以这就是 .
解析的内容.由于 pathtest.js
位于 /dir1/dir2
,这也是 __dirname
解析的内容.
但是,如果您从 /dir1
cd/dir1节点 dir2/pathtest.js
你得到
<预><代码>.=/目录1__dirname =/dir1/dir2在那种情况下,您的工作目录是 /dir1
所以这就是 .
解析为,但 __dirname
仍然解析为 /dir1/dir2
.
在 require
...
中使用 .
如果在 dir2/pathtest.js
中,你有一个 require
调用在 dir1
中包含一个文件,你会总是强>做
require('../thefile')
因为 require
中的路径总是相对于你调用它的文件.它与您的工作目录无关.
When programming in Node.js and referencing files that are located somewhere in relation to your current directory, is there any reason to use the __dirname
variable instead of just a regular ./
? I've been using ./ thus far in my code and just discovered the existence of __dirname
, and essentially want to know whether it would be smart to convert my ./'s to that, and if so, why that would be a smart idea.
The gist
In Node.js, __dirname
is always the directory in which the currently executing script resides (see this). So if you typed __dirname
into /d1/d2/myscript.js
, the value would be /d1/d2
.
By contrast, .
gives you the directory from which you ran the node
command in your terminal window (i.e. your working directory) when you use libraries like path
and fs
. Technically, it starts out as your working directory but can be changed using process.chdir()
.
The exception is when you use .
with require()
. The path inside require
is always relative to the file containing the call to require
.
For example...
Let's say your directory structure is
/dir1
/dir2
pathtest.js
and pathtest.js
contains
var path = require("path");
console.log(". = %s", path.resolve("."));
console.log("__dirname = %s", path.resolve(__dirname));
and you do
cd /dir1/dir2
node pathtest.js
you get
. = /dir1/dir2
__dirname = /dir1/dir2
Your working directory is /dir1/dir2
so that's what .
resolves to. Since pathtest.js
is located in /dir1/dir2
that's what __dirname
resolves to as well.
However, if you run the script from /dir1
cd /dir1
node dir2/pathtest.js
you get
. = /dir1
__dirname = /dir1/dir2
In that case, your working directory was /dir1
so that's what .
resolved to, but __dirname
still resolves to /dir1/dir2
.
Using .
inside require
...
If inside dir2/pathtest.js
you have a require
call into include a file inside dir1
you would always do
require('../thefile')
because the path inside require
is always relative to the file in which you are calling it. It has nothing to do with your working directory.
这篇关于node.js 中的 __dirname 和 ./有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!