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.

Check if a file exists in Node.js

+0
−0

How to check in Node.js if a file exists?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

+3
−0

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 blocking, you can also use access. 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):

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−1

You can check if a file exists using the fs module. Besides the methods already mentioned, you can use fs.stat(path[, options], callback) [1].

It's also asynchronous, and the callback returns two arguments (err, stats).

import { stat } from "node:fs";

stat("/path/to/file.ext", (err, stats) => {
  if (err) {
    // Something happened; check err.code to get details
  } else {
    // File exists
  }
});

  1. Refer to https://nodejs.org/api/fs.html#fsstatpath-options-callback ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »