How to create full path with node's fs.mkdirSync?

后端 未结 22 1923
故里飘歌
故里飘歌 2020-11-29 18:03

I\'m trying to create a full path if it doesn\'t exist.

The code looks like this:

var fs = require(\'fs\');
if (!fs.existsSync(newDest)) fs.mkdirSync         


        
22条回答
  •  误落风尘
    2020-11-29 18:34

    A more robust answer is to use use mkdirp.

    var mkdirp = require('mkdirp');
    
    mkdirp('/path/to/dir', function (err) {
        if (err) console.error(err)
        else console.log('dir created')
    });
    

    Then proceed to write the file into the full path with:

    fs.writeFile ('/path/to/dir/file.dat'....
    

提交回复
热议问题