Node.js create folder or use existing

后端 未结 14 1466
暖寄归人
暖寄归人 2020-12-04 06:43

I already have read the documentation of Node.js and, unless if I missed something, it does not tell what the parameters contain in certain operations, in particular fs.mkdi

14条回答
  •  没有蜡笔的小新
    2020-12-04 07:42

    @Liberateur's answer above did not work for me (Node v8.10.0). Little modification did the trick but I am not sure if this is a right way. Please suggest.

    // Get modules node
    const fs   = require('fs');
    const path = require('path');
    
    // Create
    function mkdirpath(dirPath)
    {
        try {
            fs.accessSync(dirPath, fs.constants.R_OK | fs.constants.W_OK);
        }
        catch(err) {
            try
            {
                fs.mkdirSync(dirPath);
            }
            catch(e)
            {
                mkdirpath(path.dirname(dirPath));
                mkdirpath(dirPath);
            }
        }
    }
    
    // Create folder path
    mkdirpath('my/new/folder/create');
    

提交回复
热议问题