Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Check if a file exists in Node.js

You can use existsSync: import { existsSync } from 'node:fs'; if (existsSync('/path/to/file')) { console.log('Found'); } else { console.log('Not found'); } If you want to avoid block...

posted 7mo ago by philipp.classen‭  ·  edited 7mo ago by philipp.classen‭

Answer
#4: Post edited by user avatar philipp.classen‭ · 2024-05-19T00:41:38Z (7 months ago)
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • import { access } from 'node:fs/promises';
  • try {
  • await access('/path/to/file');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • import { access } from 'node:fs/promises';
  • try {
  • await access('/path/to/file');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
  • Keep in mind that testing if a file exist and then opening it, is not recommended. Instead directly opening the file and handling errors is preferred ([quoting the docs](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode)):
  • > Using fsPromises.access() to check for the accessibility of a file before calling fsPromises.open() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.
#3: Post edited by user avatar philipp.classen‭ · 2024-05-19T00:32:13Z (7 months ago)
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • import { access } from 'node:fs/promises';
  • try {
  • await access('/etc/passwd');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • import { access } from 'node:fs/promises';
  • try {
  • await access('/path/to/file');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
#2: Post edited by user avatar philipp.classen‭ · 2024-05-19T00:31:05Z (7 months ago)
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • try {
  • await access('/etc/passwd');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
  • You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):
  • ```
  • import { existsSync } from 'node:fs';
  • if (existsSync('/path/to/file')) {
  • console.log('Found');
  • } else {
  • console.log('Not found');
  • }
  • ```
  • If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:
  • ```
  • import { access } from 'node:fs/promises';
  • try {
  • await access('/etc/passwd');
  • console.log('Found');
  • } catch {
  • console.error('Not found');
  • }
  • ```
#1: Initial revision by user avatar philipp.classen‭ · 2024-05-19T00:28:03Z (7 months ago)
You can use [`existsSync`](https://nodejs.org/api/fs.html#fsexistssyncpath):

```
import { existsSync } from 'node:fs';

if (existsSync('/path/to/file')) {
  console.log('Found');
} else {
  console.log('Not found');
}
```

If you want to avoid blocking, you can also use [`access`](https://nodejs.org/api/fs.html#fspromisesaccesspath-mode). If you pass no arguments, it runs in `fs.constants.F_OK` mode, which will test if the given file or directory is visible to the calling process:

```
try {
  await access('/etc/passwd');
  console.log('Found');
} catch {
  console.error('Not found');
}
```