How to use the existsSync function from graceful-fs

Find comprehensive JavaScript graceful-fs.existsSync code examples handpicked from public code repositorys.

graceful-fs.existsSync is a method used to check if a file or directory exists at a given path in a Node.js application.

48
49
50
51
52
53
54
55
56
57
58


  const relative = symlinkPathsSync(srcpath, dstpath)
  srcpath = relative.toDst
  type = symlinkTypeSync(relative.toCwd, type)
  const dir = path.dirname(dstpath)
  const exists = fs.existsSync(dir)
  if (exists) return fs.symlinkSync(srcpath, dstpath, type)
  mkdirsSync(dir)
  return fs.symlinkSync(srcpath, dstpath, type)
}
fork icon0
star icon1
watch icon1

+ 7 other calls in file

55
56
57
58
59
60
61
62
63
64
65
66
67


function mkdirsSync(path) {
  if (!path) throw new TypeError('path is required!');


  const parent = dirname(path);
  const exist = fs.existsSync(parent);


  if (!exist) mkdirsSync(parent);
  fs.mkdirSync(path);
}
fork icon0
star icon1
watch icon0

+ 15 other calls in file

How does graceful-fs.existsSync work?

graceful-fs.existsSync is a method provided by the graceful-fs module that extends the native fs module in Node.js to provide additional functionality with improved error handling. graceful-fs.existsSync is used to check if a file or directory exists at a given path in a Node.js application. It takes a path as its argument and returns a boolean value that indicates whether a file or directory exists at the specified path or not. If the path exists and refers to a directory, then the method returns false. If the path exists and refers to a file, then the method returns true. graceful-fs.existsSync uses a synchronous implementation to check for the existence of the path. It first checks if the path exists in the in-memory cache. If the path is not found in the cache, it makes a system call to check if the path exists on the file system. The method returns true if the path exists, otherwise it returns false. Note that because graceful-fs.existsSync is synchronous, it blocks the execution of the program until it completes. This can lead to performance issues in certain scenarios, such as when checking for the existence of large numbers of files or directories. In such cases, it is recommended to use asynchronous methods like graceful-fs.stat or graceful-fs.access.

219
220
221
222
223
224
225
226
227
228
229
					  ],
					}
				`);
});


const isCaseInsensitiveFilesystem = fs.existsSync(
	path.resolve(__dirname, "fixtures", "errors", "FILE.js")
);
if (isCaseInsensitiveFilesystem) {
	it("should emit warning for case-preserved disk", async () => {
fork icon163
star icon0
watch icon24

+ 13 other calls in file

45
46
47
48
49
50
51
52
53
54
  err.message = err.message.replace('lstat', 'ensureLink')
  throw err
}

const dir = path.dirname(dstpath)
const dirExists = fs.existsSync(dir)
if (dirExists) return fs.linkSync(srcpath, dstpath)
mkdir.mkdirsSync(dir)

return fs.linkSync(srcpath, dstpath)
fork icon0
star icon0
watch icon1

+ 11 other calls in file

Ai Example

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

if (fs.existsSync("/path/to/file.txt")) {
  console.log("File exists");
} else {
  console.log("File does not exist");
}

In this example, we first require the graceful-fs module and then call the existsSync method on the fs object, passing the file path as an argument. If the file exists, the function returns true and we log "File exists". If the file does not exist, the function returns false and we log "File does not exist".

98
99
100
101
102
103
104
105
106
107
108
109
    return Function('return this')();
  }
})();


var jestExistsFile =
  global[Symbol.for('jest-native-exists-file')] || fs.existsSync;


function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
fork icon0
star icon0
watch icon1

28
29
30
31
32
33
34
35
36
37
38
39
    })
  })
}


function createLinkSync (srcpath, dstpath, callback) {
  var destinationExists = fs.existsSync(dstpath)
  if (destinationExists) return undefined


  try {
    fs.lstatSync(srcpath)
fork icon0
star icon0
watch icon1

+ 19 other calls in file

44
45
46
47
48
49
50
51
52
53
54


  var relative = symlinkPathsSync(srcpath, dstpath)
  srcpath = relative.toDst
  type = symlinkTypeSync(relative.toCwd, type)
  var dir = path.dirname(dstpath)
  var exists = fs.existsSync(dir)
  if (exists) return fs.symlinkSync(srcpath, dstpath, type)
  mkdirsSync(dir)
  return fs.symlinkSync(srcpath, dstpath, type)
}
fork icon0
star icon0
watch icon1

+ 17 other calls in file

14
15
16
17
18
19
20
21
22
23
24
25
26
	fs.mkdirSync(database_path, 0777);
}




function file_exists(path) {
	return fs.existsSync(path);
}


function file_size(fd) {
	return fs.fstatSync(fd).size;
fork icon0
star icon0
watch icon0