Javascript 跨操作系统平台的路径分隔符
从fs.mkdtemp上的nodejsJavascript 跨操作系统平台的路径分隔符,javascript,node.js,path,operating-system,temp,Javascript,Node.js,Path,Operating System,Temp,从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.s
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
和temp目录有关,我希望代码与平台无关,并且能够在多个平台上运行
path.sep
的值是多少/tmp
在windows上可用吗感谢使用
os.tmpDir
和路径。加入跨平台代码的函数
var tmp = require('os').tmpDir();
var dest = path.join(tmp, "com.domain.app");
!fs.existsSync(dest) ? fs.mkdirSync(dest) : null;
参考资料