How to use the accessSync function from fs

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

fs.accessSync checks if the user has the necessary permissions to access a file or directory in a synchronous way.

7
8
9
10
11
12
13
14
15
16

const fs = require('fs');
const co = require('./co');

exports.access = co.promisify(fs.access);
exports.accessSync = fs.accessSync;
exports.appendFile = co.promisify(fs.appendFile);
exports.appendFileSync = fs.appendFileSync;
exports.chmod = co.promisify(fs.chmod);
exports.chmodSync = fs.chmodSync;
fork icon22
star icon45
watch icon26

5054
5055
5056
5057
5058
5059
5060
5061
5062
5063

child.stdin.end();

if (argv.o) {
  try {
    fs.accessSync(argv.o);
    errx('File ' + argv.o + ' already exists.');
  } catch (error) {
    /* We are fine, not replacing a file probably. */
  }
fork icon14
star icon8
watch icon15

+ 3 other calls in file

How does fs.accessSync work?

fs.accessSync() is a synchronous version of the fs.access() method, which is used to check the user's permissions for a specific file or directory path in a synchronous manner, without any callback functions. If the user has the required permission, it returns undefined, and if not, it throws an error.

2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
})

let execute_chk = execute_chk_div.querySelector('.checkbox')

try {
    fs.accessSync(filename, fs.constants.X_OK)
    execute_chk.checked = 1
} catch (err) {
    execute_chk.checked = 0
}
fork icon2
star icon5
watch icon1

+ 3 other calls in file

198
199
200
201
202
203
204
205
206
207
let modpackFolder = join(this.minecraftpath, "modpack", chapter.title)
if(!fs.existsSync(modpackFolder))
    fs.mkdirSync(modpackFolder, { recursive: true })
const path = join(modpackFolder, `modpack${j}.zip`)
try {
    fs.accessSync(path, constants.W_OK)
    let sha1 = await hasha.fromFile(path, {algorithm: "sha1"})
    if(sha1 === chapter.modspack.sha1sum[j]) {
        await this.unzipMods(path).catch(err => {
            reject(err)
fork icon1
star icon2
watch icon3

+ 6 other calls in file

Ai Example

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

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

In this example, fs.accessSync is used to check if a file located at /path/to/file exists and is readable. The second argument (fs.constants.R_OK) specifies the type of accessibility check to be performed. If the file is readable, the message 'File is readable.' is logged to the console. If the file is not readable, the message 'File is not readable.' is logged to the console.

16
17
18
19
20
21
22
23
24
25
26
27
}
catch (err) { manifest = { "files": {} };}


//cleanup manifest
Object.keys(manifest.files).forEach(entry => {
	try { fs.accessSync(path.join(__dirname, entry), fs.constants.F_OK) }
	catch (err) { delete manifest.files[entry]; }
});


//recursive file gather
fork icon0
star icon1
watch icon1

68
69
70
71
72
73
74
75
76
77
certObj.paths = {};
certObj.certs = {};
if (key) {
    const keypath = key.startsWith('/') ? key : `${basePath}/${key}`;
    assert.doesNotThrow(() =>
        fs.accessSync(keypath, fs.F_OK | fs.R_OK),
        `File not found or unreachable: ${keypath}`);
    certObj.paths.key = keypath;
    certObj.certs.key = fs.readFileSync(keypath, 'ascii');
}
fork icon236
star icon0
watch icon89

+ 5 other calls in file

198
199
200
201
202
203
204
205
206
207
 * @param pathToElement
 * @return {boolean}
 */
access(pathToElement) {
  try {
    fs.accessSync(pathToElement.toString());
    return true;
  } catch (e) {
    return false;
  }
fork icon0
star icon4
watch icon2

60
61
62
63
64
65
66
67
68
69
subdir.pop();
var l10n = 'src/views/' + subdir.join('/') + '/l10n.json';
var name = routes[v].name;
try {
    // only Initialize if there is an l10n file
    fs.accessSync(l10n);
    // TODO: ES6
    // var tx_resource = `scratch-website.${name}-l10njson`;
    // cmd = `tx set --auto-local --source-lang en --type KEYVALUEJSON -r ${tx_resource}` +
    // ` \'localizations/${name}/<lang>.json\' --source-file ${l10n} ${execute}`;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

68
69
70
71
72
73
74
75
76
77
78
79
}


var localesDir = path.resolve(__dirname, '../', args.shift());


try {
    fs.accessSync(localesDir, fs.F_OK);
} catch (err) {
    // Doesn't exist - create it.
    process.stdout.write('Fatal error: No localizations directory.\n');
    process.exit(1);
fork icon0
star icon0
watch icon1

+ 9 other calls in file

110
111
112
113
114
115
116
117
118
119
120
 */
function doesPathExist(pathToVerify) {
    let pathExists = true;


    try {
        fs.accessSync(pathToVerify);
    } catch (error) {
        pathExists = false;
    }

fork icon0
star icon0
watch icon1

621
622
623
624
625
626
627
628
629
630

if (!fs.existsSync(file)) { return false; } // exists
if (!fs.lstatSync(file).isFile()) { return false; } // isfile

try {
    fs.accessSync(file, fs.constants.W_OK);
} catch (err) {
    console.log('Opening File in read-only: ' + file);
    console.log(err);
    simpleEdit.modal.open({
fork icon0
star icon0
watch icon1

260
261
262
263
264
265
266
267
268
269
var translationFromMap = json5.parse(fs.readFileSync(path.resolve(__dirname, 'lib', 'Language', 'en', 'translation.json'), 'utf8')) || {};
var translation = {...translationFromLib, ...translationFromMap};
fs.writeFileSync(translationFromLibPath, JSON.stringify(translation, null, 2));

try {
    fs.accessSync(templateDir);
} catch (e) {
    // Datasources directory doesn't exist? No problem.
    done();
    return;
fork icon0
star icon0
watch icon1

65
66
67
68
69
70
71
72
73
74
  filename = filename.replace('.', `_${kk}.`)
}

// check if file already exists
try {
  fs.accessSync(`./${username}/${filename}`);
  console.log(`File ${filename} already exists.`);
  console.log(`Skipping ${username}`);
  return;
} catch (error) {}
fork icon0
star icon0
watch icon1