随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
![](https://img-blog.csdnimg.cn/20181127214229553.jpg)
from:http://www.cnblogs.com/huanghu/p/5586805.html
比如在当前目录下创建目录/tmp/test/,过程如下:
开始我这样写的:
1 2 3 4 5 6 7 | console.log( "创建目录 //test/" ); fs.mkdir( "./tmp/test/" ,function(err){ if (err) { return console.error(err); } console.log( "目录创建成功。" ); }); |
然后运行抛出异常(no such file or directory,mkdir...)如下:
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvOTU0NzEzLzIwMTYwNi85NTQ3MTMtMjAxNjA2MTUxMTEzMjQ3NzYtMTg0OTEwNzc4LnBuZw?x-oss-process=image/format,png)
后来发现目录要一层一层创建,修改后代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | console.log( "创建目录 /tmp/test/" ); //异步 fs.mkdir( "./tmp/" , function (err){ if (err) { return console.error(err); } console.log( "tmp目录创建成功。" ); fs.mkdir( "./tmp/test/" , function (err){ if (err) { return console.error(err); } console.log( "test目录创建成功。" ); }); }); console.log( "创建目录 /tmp1/test/" ); //同步 fs.mkdirSync( "./tmp1/" ); fs.mkdirSync( "./tmp1/test/" ); |