私はNode.jsをいじってみましたが、少し問題があります。というディレクトリにあるスクリプトがありますdata。スクリプトで、サブディレクトリ内のdataサブディレクトリにあるファイルにデータを書き込みます。ただし、次のエラーが発生します。
{ [Error: ENOENT, open 'D:\data\tmp\test.txt'] errno: 34, code: 'ENOENT', path: 'D:\\data\\tmp\\test.txt' }
コードは次のとおりです。
var fs = require('fs');
fs.writeFile("tmp/test.txt", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
ファイルに書き込むために終了しない場合、Node.jsがディレクトリ構造を作成する方法を見つけるのを手伝ってくれる人はいますか?
fs.promises.mkdir(path.dirname("tmp/test.txt"), {recursive: true}).then(x => fs.promises.writeFile("tmp/test.txt", "Hey there!"))