3 个 Node 常规操作

获取 cpu 核数

const numCPUs = require('os').cpus().length;

console.log(numCPUs);

获取 node 版本

console.log(process.version);

无视上级目录创建目录和文件

if Node > 10.12.0 :

require("fs").mkdirSync(fileDir, {

recursive: true,

});

require("fs").writeFileSync(filepath, content);

else

// use before writeFileSync

function ensureDirectoryExistence(filePath) {

var dirname = path.dirname(filePath);

if (require('fs').existsSync(dirname)) {

return true;

}

ensureDirectoryExistence(dirname);

require('fs').mkdirSync(dirname);

}

参考:https://stackoverflow.com/questions/13542667/create-directory-when-writing-to-file-in-node-js

以上是 3 个 Node 常规操作 的全部内容, 来源链接: utcz.com/z/264571.html

回到顶部