node.js mkdir enonent when passed a string


node.js mkdir enonent when passed a string



I'm trying to build folders with a string, but I keep erroring out, everything looks good, but I'm obviously missing something, any help would be great.


var setFile = 'dijit/analysis/images/'
folders = setFile.substr(0,setFile.lastIndexOf('/'));

fs.mkdir(folders, function(err){
console.log('problem: ' + err);
});



Error: Error: ENOENT, mkdir 'dijit/analysis/images'


Error: ENOENT, mkdir 'dijit/analysis/images'



Thanks,





Do the dijit and dijit/analysis directories already exist? Are you perhaps looking for npmjs.org/package/mkdirp?
– loganfsmyth
Mar 28 '14 at 6:16


dijit


dijit/analysis




2 Answers
2



fs.mkdir can able to built only a single folder. You are trying to create a folder inside a non existing folder. This problem can be solved by fs-extra module of npm. Following code should fulfill your need.


var setFile = 'dijit/analysis/images/',
fsExtra = require('fs-extra'),
folders = setFile.substr(0, setFile.lastIndexOf('/'));

fsExtra.mkdirp(folders, function(err) {
console.log('problem: ' + err);
});



Kundu's comment answers this problem, but if you want a different solution there is plenty over at How to create full path with node's fs.mkdirSync?






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?