fs 模块函数使用的当前目录是什么?

What is the current directory used by fs module functions?

典型node.js/Expressapp调用fs模块中的方法时,当前目录是什么?例如:

var fs = require('fs');
var data = fs.readFile("abc.jpg", "binary");

用于定位 abc.jpg 的默认文件夹是什么?它取决于包含脚本的文件夹位置吗?

有没有查询当前目录的方法?


我的文件结构是:

ExpressApp1/
           app.js
           routes/
                 members.js

在 members.js 我有一个 fs.createWriteStream("abc") 文件 abc 是在 ExpressApp1/

创建的

这是节点解释器启动的目录(又名当前工作目录),而不是脚本文件夹位置的目录(谢谢 robertklep)。

另外,当前工作目录可以通过以下方式获取:

process.cwd()

更多信息,您可以查看:https://nodejs.org/api/fs.html

编辑:因为您在 ExpressApp1 目录中通过 node app.js 启动了 app.js,每个相对路径都将相对于 "ExpressApp1" 文件夹,这就是为什么 fs.createWriteStream("abc")将创建写入流以写入 ExpressApp1/abc 中的文件。如果要写入ExpressApp1/routes/abc,可以将members.js中的代码改成:

fs.createWriteStream(path.join(__dirname, "abc"));

更多:

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

https://nodejs.org/docs/latest/api/path.html#path_path_join_paths