path分隔符跨os平台
来自fs.mkdtemp上的nodejs 文档
const tmpDir = '/tmp'; const subdir = '/com.domain.app'; !fs.existsSync(tmp + subdir) ? fs.mkdirSync(tmp + subdir) : null; // This method is *CORRECT*: const path = require('path'); fs.mkdtemp(tmpDir + path.sep + subdir + path.sep, function(err, folder){ if (err) throw err; console.log(folder); });
我的问题是关于path.sep
和临时目录,我希望代码是平台不可知的,并能够在多个平台上运行。
- 在所有nodejs平台上,
path.sep
的价值是什么。 - 所有nodejs平台上tmp目录的价值是什么?
- 是
/tmp
在Windows上可用? - 当涉及到临时目录/path时,上述代码的任何build议
谢谢
跨平台代码使用os.tmpDir
和path.join
函数。
var tmp = require('os').tmpDir(); var dest = path.join(tmp, "com.domain.app"); !fs.existsSync(dest) ? fs.mkdirSync(dest) : null;
参考。 编写跨平台节点