如果目录不存在,下面的方法是否正确?
它应该对脚本具有完全的权限,并且其他人可以阅读。
var dir = __dirname + '/upload';
if (!path.existsSync(dir)) {
fs.mkdirSync(dir, 0744);
}
如果目录不存在,下面的方法是否正确?
它应该对脚本具有完全的权限,并且其他人可以阅读。
var dir = __dirname + '/upload';
if (!path.existsSync(dir)) {
fs.mkdirSync(dir, 0744);
}
当前回答
Fs.exist()已弃用。所以我使用fs.stat()来检查目录状态。如果目录不存在,fs.stat()抛出一个错误,并显示类似'没有这样的文件或目录'的消息。然后我创建了一个目录。
const fs = require('fs').promises;
const dir = './dir';
fs.stat(dir).catch(async (err) => {
if (err.message.includes('no such file or directory')) {
await fs.mkdir(dir);
}
});
其他回答
mkdir方法能够递归地在一个路径中创建任何不存在的目录,并忽略存在的目录。
来自Node.js v10/11文档:
// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});
注意:首先需要导入内置的fs模块。
现在,这里有一个更健壮的例子,它利用了原生ECMAScript模块(启用了标志和.mjs扩展名),处理非根路径,并说明完整的路径名:
import fs from 'fs';
import path from 'path';
function createDirectories(pathname) {
const __dirname = path.resolve();
pathname = pathname.replace(/^\.*\/|\/?[^\/]+\.[a-z]+|\/$/g, ''); // Remove leading directory markers, and remove ending /file-name.extension
fs.mkdir(path.resolve(__dirname, pathname), { recursive: true }, e => {
if (e) {
console.error(e);
} else {
console.log('Success');
}
});
}
你可以像createDirectories('/components/widget/widget.js');一样使用它。
当然,当目录创建时,你可能想要在async/await中使用promises以一种更可读的同步方式来利用文件创建;但是,这超出了问题的范围。
单行版本:
// Or in TypeScript: import * as fs from 'fs';
const fs = require('fs');
!fs.existsSync(dir) && fs.mkdirSync(dir);
如果文件夹存在,您可以使用mkdir并捕获错误。 这是异步的(因此是最佳实践)并且安全。
fs.mkdir('/path', err => {
if (err && err.code != 'EEXIST') throw 'up'
.. safely do your stuff here
})
(可选地使用mode添加第二个参数。)
其他的想法:
You could use then or await by using native promisify. const util = require('util'), fs = require('fs'); const mkdir = util.promisify(fs.mkdir); var myFunc = () => { ..do something.. } mkdir('/path') .then(myFunc) .catch(err => { if (err.code != 'EEXIST') throw err; myFunc() }) You can make your own promise method, something like (untested): let mkdirAsync = (path, mode) => new Promise( (resolve, reject) => mkdir (path, mode, err => (err && err.code !== 'EEXIST') ? reject(err) : resolve() ) ) For synchronous checking, you can use: fs.existsSync(path) || fs.mkdirSync(path) Or you can use a library, the two most popular being mkdirp (just does folders) fsextra (supersets fs, adds lots of useful stuff)
不,原因有很多。
The path module does not have an exists/existsSync method. It is in the fs module. (Perhaps you just made a typo in your question?) The documentation explicitly discourage you from using exists. fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code. In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there. Since we're talking about a directory rather than a file, this advice implies you should just unconditionally call mkdir and ignore EEXIST. In general, you should avoid the *Sync methods. They're blocking, which means absolutely nothing else in your program can happen while you go to the disk. This is a very expensive operation, and the time it takes breaks the core assumption of node's event loop. The *Sync methods are usually fine in single-purpose quick scripts (those that do one thing and then exit), but should almost never be used when you're writing a server: your server will be unable to respond to anyone for the entire duration of the I/O requests. If multiple client requests require I/O operations, your server will very quickly grind to a halt. The only time I'd consider using *Sync methods in a server application is in an operation that happens once (and only once), at startup. For example, require actually uses readFileSync to load modules. Even then, you still have to be careful because lots of synchronous I/O can unnecessarily slow down your server's startup time. Instead, you should use the asynchronous I/O methods.
所以如果我们把这些建议放在一起,我们会得到这样的结果:
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // Allow the `mask` parameter to be optional
cb = mask;
mask = 0o744;
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // Ignore the error if the folder already exists
else cb(err); // Something else went wrong
} else cb(null); // Successfully created folder
});
}
我们可以这样使用它:
ensureExists(__dirname + '/upload', 0o744, function(err) {
if (err) // Handle folder creation error
else // We're all good
});
当然,这并不能解释边缘情况,比如
如果在程序运行时删除文件夹会发生什么?(假设在启动过程中只检查它是否存在一次) 如果文件夹已经存在,但是权限错误,会发生什么?
var dir = 'path/to/dir';
try {
fs.mkdirSync(dir);
} catch(e) {
if (e.code != 'EEXIST') throw e;
}