How to use the accessSync function from fs-extra

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

fs-extra.accessSync is a method used to check if the user has the appropriate permissions to read and/or write a file or directory.

347
348
349
350
351
352
353
354
355
356
// Build path to the default icon for this host
var iconPath = path.join(__dirname, '../../assets/icons/' + crxConfig.host + '.png');

try {
    // Check if icon exists
    fs.accessSync(iconPath, fs.F_OK);
}
catch (err) {
    // No such file
    return null;
fork icon86
star icon477
watch icon0

347
348
349
350
351
352
353
354
355
356
    }

} else {

    try {
        fs.accessSync(this.customFolder, fs.constants.W_OK)
    } catch (e) {
        log.error(`[uibuilder:nodeInstance:${this.url}] Local custom folder ERROR`, e.message)
        customFoldersOK = false
    }
fork icon75
star icon352
watch icon25

+ 25 other calls in file

How does fs-extra.accessSync work?

fs-extra.accessSync() is a synchronous function provided by the fs-extra library that checks if a file or directory can be accessed by the calling process. The function takes two arguments: path, which is the path of the file or directory to be checked, and mode, which is optional and can be used to specify the type of access check to be performed (e.g. read, write, execute). If the file or directory can be accessed according to the specified mode, the function returns undefined. Otherwise, it throws an error indicating the reason for the access failure.

718
719
720
721
722
723
724
725
726
727

// Cope with pre v4.1 node configs (sourceFolder not defined)
if ( node.sourceFolder === undefined ) {
    try {
        // Check if local dist folder contains an index.html & if NR can read it - fall through to catch if not
        fs.accessSync( path.join(node.customFolder, 'dist', defaultPageName), fs.constants.R_OK )
        // If the ./dist/index.html exists use the dist folder...
        customStatic = 'dist'
        log.trace(`[uibuilder:web:setupInstanceStatic:${node.url}] Using local dist folder`)
        // NOTE: You are expected to have included vendor packages in
fork icon75
star icon346
watch icon24

+ 25 other calls in file

2
3
4
5
6
7
8
9
10
11
const accessAsync = (path) => 
  new Promise(resolve => fs.access(path, fs.F_OK, err => resolve(!err)));

const accessSync = (path) => {
  try {
    fs.accessSync(path)
    return true;
  } catch(e) {
    return false;
  }
fork icon0
star icon2
watch icon1

Ai Example

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

try {
  fs.accessSync("/path/to/file", fs.constants.R_OK);
  console.log("File is readable");
} catch (err) {
  console.error("File is not readable");
}

In the above example, fs.accessSync is used to check if a file located at /path/to/file is readable. If the file is readable, the message "File is readable" is logged to the console. If the file is not readable, the error message "File is not readable" is logged to the console.

8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
const overwrite = options.overwrite || options.clobber || false

src = path.resolve(src)
dest = path.resolve(dest)

if (src === dest) return fs.accessSync(src)

if (isSrcSubdir(src, dest)) throw new Error(`Cannot move '${src}' into itself '${dest}'.`)

mkdirpSync(path.dirname(dest))
fork icon0
star icon1
watch icon0

+ 8 other calls in file

59
60
61
62
63
64
65
66
67
68
  return cp.spawnSync(cmd, args, opts);
}

function exists(path) {
  try {
    fs.accessSync(path);
    return true;
  } catch (e) {
    return false;
  }
fork icon0
star icon1
watch icon2

function icon

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