How to use the constants function from fs

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

fs.constants is an object in Node.js that provides various constants related to file system operations.

16
17
18
19
20
21
22
23
24
25
exports.chmodSync = fs.chmodSync;
exports.chown = co.promisify(fs.chown);
exports.chownSync = fs.chownSync;
exports.close = co.promisify(fs.close);
exports.closeSync = fs.closeSync;
exports.constants = fs.constants;
exports.createReadStream = fs.createReadStream;
exports.createWriteStream = fs.createWriteStream;
exports.exists = async (file) => {
  try {
fork icon22
star icon45
watch icon26

39
40
41
42
43
44
45
46
47
48
49
50
fs.mkdirSync(existingDir);
fs.writeFileSync(existingFile, 'test', 'utf-8');
fs.writeFileSync(existingFile2, 'test', 'utf-8');




const { COPYFILE_EXCL } = fs.constants;
const { internalBinding } = require('internal/test/binding');
const {
  UV_EBADF,
  UV_EEXIST,
fork icon42
star icon19
watch icon0

How does fs.constants work?

fs.constants is an object in Node.js that provides constant values for use with file system operations, such as file permissions, file modes, file types, and error codes. These constants are used with various file system methods in the fs module, such as fs.access(), fs.chmod(), fs.createWriteStream(), fs.stat(), and more. Some of the constants in fs.constants include fs.constants.F_OK for checking file existence, fs.constants.W_OK for checking write permissions, fs.constants.S_IFMT for file type masks, and fs.constants.EEXIST for file exists errors.

194
195
196
197
198
199
200
201
202
203
204
205
  }
}


const canAccessFolder = async function (file) {
  const fsAccess = util.promisify(fs.access);
  await fsAccess(file, fs.constants.R_OK | fs.constants.W_OK);
};


const cypressRunner = async function (nodeBin, runCfgPath, suiteName, timeoutSec, preExecTimeoutSec) {
  runCfgPath = getAbsolutePath(runCfgPath);
fork icon8
star icon3
watch icon5

230
231
232
233
234
235
236
237
238
239
*
* 2. Download Core
*
**************************************************************************************************************/
let engine = this.cores[core]
let exists = s => new Promise(r=>fs.access(s, fs.constants.F_OK, e => r(!e)))
let e = await exists(path.resolve(engine.home));
if (e) {
  console.log("try fetching", engine.home, engine.url)
  await git.fetch({ fs, http, dir: engine.home, url: engine.url })
fork icon110
star icon0
watch icon16

+ 2 other calls in file

Ai Example

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

// Check if a file exists using fs.access with fs.constants.F_OK
fs.access("example.txt", fs.constants.F_OK, (err) => {
  if (err) {
    console.error("File does not exist");
  } else {
    console.log("File exists");
  }
});

In this example, fs.constants.F_OK is used to specify that we want to check if the file exists.

19
20
21
22
23
24
25
26
27
28
* The following constants define the possible values of mode.
* It is possible to create a mask consisting of the bitwise OR of two or more values.
* * `fs.constants.F_OK` - path is visible to the calling process. This is useful for determining if a file exists, but says nothing about rwx permissions. Default if no mode is specified.
* * `fs.constants.R_OK` - path can be read by the calling process.
* * `fs.constants.W_OK` - path can be written by the calling process.
* * `fs.constants.X_OK` - path can be executed by the calling process. This has no effect on Windows (will behave like fs.constants.F_OK).
* @returns Returns `true` if the file is accessible or `false` if the file is inaccessible.
*
* @example
* The following example checks if the file `/etc/passwd` can be read and written by the current process.
fork icon3
star icon0
watch icon1

+ 5 other calls in file

174
175
176
177
178
179
180
181
182
183
const { eventName, image } = req.params;

let compressedExists;

try {
  await fs.access(path.join(__dirname, `../uploads/images/media/${eventName}/thumb_${image}`), rawFs.constants.F_OK);
  compressedExists = true;
} catch (error) {
  compressedExists = false;
}
fork icon0
star icon2
watch icon2

111
112
113
114
115
116
117
118
119
120
121


function getProxy(realNamespace, methodPaths, methodName, sandboxDirs) {
	switch (methodName) {
		case 'access':
		case 'accessSync': return function (...args) {
			if (args[1] & fs.constants.W_OK) {
				verify(args[0], sandboxDirs);
			}
			return realNamespace[methodName](...args);
		};
fork icon0
star icon0
watch icon0

+ 3 other calls in file

78
79
80
81
82
83
84
85
86
87
// 	they('should fail at overwriting goodFileNew when COPYFILE_EXCL mode is specified', async (__method__) => {
// 		await withTempFiles(async () => {
// 			fs.writeFileSync(goodFileNew, 'existing', 'utf8');
// 
// 			const unbox = sandboxFs(sandboxDir);
// 			const result = await __method__(goodFile, goodFileNew, fs.constants.COPYFILE_EXCL);
// 			unbox();
// 			
// 			assert.equal(result.code, 'OUTSIDE_SANDBOX');
// 			assert.equal(fs.readFileSync(goodFileNew, 'utf8'), 'existing');
fork icon0
star icon0
watch icon0