使用节点 REPL 记录 __dirname 的值
Using the Node REPL to log the value of __dirname
如果我写一个包含 console.log(__dirname);
的脚本,记录 __dirname
的值。但是如果我像这样对 Node REPL 进行同样的尝试:
> console.log(__dirname)
ReferenceError: __dirname is not defined
想法?
由此What is the difference between __dirname and ./ in node.js? written by d512
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.
文档说 __dirname
等于 path.dirname
。如果您在排斥中输入 path.dirname
,它会告诉您:
> console.log(path.dirname)
[Function: dirname]
undefined
现在我的猜测是:因为它是一个 repl,所以您没有存储在磁盘某处的文件。它只是读取命令,对其进行评估并将其打印到控制台。
对 REPLS 有更多经验的人可能会给出更长更详细的答案,但我认为这阐明了概念。
如果我写一个包含 console.log(__dirname);
的脚本,记录 __dirname
的值。但是如果我像这样对 Node REPL 进行同样的尝试:
> console.log(__dirname)
ReferenceError: __dirname is not defined
想法?
由此What is the difference between __dirname and ./ in node.js? written by d512
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.
文档说 __dirname
等于 path.dirname
。如果您在排斥中输入 path.dirname
,它会告诉您:
> console.log(path.dirname)
[Function: dirname]
undefined
现在我的猜测是:因为它是一个 repl,所以您没有存储在磁盘某处的文件。它只是读取命令,对其进行评估并将其打印到控制台。
对 REPLS 有更多经验的人可能会给出更长更详细的答案,但我认为这阐明了概念。