How to use the access function from fs-extra

Find comprehensive JavaScript fs-extra.access code examples handpicked from public code repositorys.

In Node.js, fs-extra.access is a function that checks whether a file or directory exists and the calling process has the necessary permissions to access it.

351
352
353
354
355
356
357
358
359
360
const projectDirectory = process.argv[4]

await Promise.all([
  updatePackageJson(path.join(projectDirectory, 'package.json')),
  fs.writeFile(path.join(projectDirectory, '.npmrc'), npmrc, 'utf8'),
  fs.access(path.join(projectDirectory, '.gitignore'))
    .catch(() => fs.writeFile(path.join(projectDirectory, '.gitignore'), gitignore, 'utf8'))
])

await require('../migrator').migrate()
fork icon234
star icon281
watch icon77

+ 2 other calls in file

362
363
364
365
366
367
368
369
370
371
 * Adds an about page to site if not present.
 */
async addAboutPage() {
  const aboutPath = path.join(this.rootPath, ABOUT_MARKDOWN_FILE);
  try {
    await fs.access(aboutPath);
  } catch (error) {
    if (fs.existsSync(aboutPath)) {
      return;
    }
fork icon106
star icon94
watch icon13

How does fs-extra.access work?

fs-extra.access is a function in the fs-extra library for Node.js that checks whether a file or directory exists and the calling process has the necessary permissions to access it.

When fs-extra.access is called with a file path as input, it performs the following operations:

  • It checks whether the file exists.
  • It checks whether the calling process has the necessary permissions to access the file, using the fs.access method with the appropriate flags.

If the file exists and the calling process has the necessary permissions to access it, fs-extra.access returns undefined. If the file does not exist or the calling process does not have the necessary permissions to access it, fs-extra.access throws an error.

Note that fs-extra.access can also be used with a directory path as input, in which case it checks whether the directory exists and the calling process has the necessary permissions to access it.

By using fs-extra.access, developers can easily check whether a file or directory exists and the calling process has the necessary permissions to access it before attempting to read or modify it.

366
367
368
369
370
371
372
373
374
375
case 'checkfolder': {
    log.trace(`[uibuilder:adminRouterV3:GET:checkfolder] See if a node's custom folder exists. URL: ${params.url}`)

    const folder = path.join( uib.rootFolder, params.url)

    fs.access(folder, fs.constants.F_OK)
        .then( () => {
            res.statusMessage = 'Folder checked'
            res.status(200).json( true )
            return true
fork icon75
star icon346
watch icon24

+ 12 other calls in file

221
222
223
224
225
226
227
228
229
230
})
```

#### 文件系统模块apis

- fs.access():检查文件是否存在,Node.js可以使用其权限访问它
- fs.appendFile():将数据附加到文件。如果文件不存在,则创建它
- fs.chmod():更改通过传递的文件名指定的文件的权限。相关阅读:fs.lchmod(),fs.fchmod()
- fs.chown():更改由传递的文件名指定的文件的所有者和组。相关阅读:fs.fchown(),fs.lchown()
- fs.close():关闭文件描述符
fork icon0
star icon12
watch icon1

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const fs = require("fs-extra");

const filePath = "/path/to/file.txt";

fs.access(filePath, fs.constants.R_OK, (err) => {
  if (err) {
    console.error(`Unable to access file: ${filePath}`);
  } else {
    console.log(`File exists and is readable: ${filePath}`);
  }
});

In this example, we're using fs-extra.access to check whether a file at /path/to/file.txt exists and is readable. We call fs.access with the file path and the fs.constants.R_OK flag, which specifies that we want to check whether the file is readable. If the file exists and is readable, fs.access does not throw an error, and we log a message to the console indicating that the file exists and is readable. If the file does not exist or is not readable, fs.access throws an error, and we log a message to the console indicating that we are unable to access the file. Note that fs-extra.access can also be used with a directory path as input, and with different flags to check for write or execute permissions.

574
575
576
577
578
579
580
581
582
583
Promise.all(['index.js', 'index.html', 'index.jade', 'index.css', 'index.styl'].map(function(fileName){
    if(!fs.constants){
        // @ts-ignore
        fs.constants = fs;
    }
    return fs.access(pathName+'/'+fileName,fs.constants.F_OK).then(function(){
        throw new Error(fileName+" must not exists in private path: "+pathName);
    },function(err){
        if(err.code!=='ENOENT'){
            console.log("error",err);
fork icon3
star icon4
watch icon10

+ 11 other calls in file

8332
8333
8334
8335
8336
8337
8338
8339
8340
  })
})

function doRename () {
  if (path.resolve(src) === path.resolve(dest)) {
    fs.access(src, callback)
  } else if (overwrite) {
    fs.rename(src, dest, err => {
      if (!err) return callback()
fork icon0
star icon1
watch icon0

+ 17 other calls in file

232
233
234
235
236
237
238
239
240
241
) {
	const screenReaderPath = getScreenReaderDirectory(
		screenReadersPath,
		screenReader
	);
	const screenReaderExists = await fs.access(screenReaderPath).then(
		() => true,
		() => false
	);
	if (screenReaderExists) {
fork icon0
star icon0
watch icon1

+ 9 other calls in file

53
54
55
56
57
58
59
60
61
62
63
64


const AddingNewsPaper = async (req, res, next) => {
  const CreateFolder = (value) => {
    const path = value;


    fs.access(path, (error) => {
      if (error) {
        fs.mkdir(path, { recursive: true }, (error) => {
          if (error) {
            console.log(error);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

68
69
70
71
72
73
74
75
76
77
78
    return _.done(result, callback)
  })
}


function exists(src, callback) {
  return fs.access(src, fs.constants.F_OK, (err) => {
    return err ? _.fail(err, callback) : _.done(src, callback)
  })
}

fork icon0
star icon0
watch icon0

+ 9 other calls in file

44
45
46
47
48
49
50
51
52
53
54
    return path.resolve(this.dir, ...parts);
  }
}


async function fileReadable(filePath) {
  return fsx.access(filePath, fsx.constants.R_OK).then(
    () => true,
    () => false
  );
}
fork icon0
star icon0
watch icon0

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)