Home > Web Front-end > JS Tutorial > body text

How does the node fs module detect whether a file exists?

青灯夜游
Release: 2021-12-13 12:01:55
Original
4775 people have browsed it

Detection method: 1. Use the "const fs = require("fs");" statement to introduce the fs module; 2. Use the exists() method of the fs module to detect whether the specified file exists in the file system. Syntax "fs.exists(path,callback)".

How does the node fs module detect whether a file exists?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

node fs module detects whether the file exists

1. First, you need to introduce the fs module

const fs = require("fs");
Copy after login

2. Use the fs module Detection

fs.exists(filePath, (exists) => {      if (exists) {
          console.log("文件已存在");
     } else {
          console.log("文件不存在");
     }
});
Copy after login

Note:

fs.exists() method is the built-in application programming interface of the fs module, which provides an API. Used to interact with the file system in a tightly modeled way around standard POSIX functions. The fs.exists() method is used to test whether a given path exists in the file system.

Usage:

fs.exists( path, callback )
Copy after login

Parameters: This method accepts the two parameters mentioned above and below:

  • #path: To test whether the directory exists path. It can be a string, buffer, etc.

  • callback: It is the callback function passed to the exists() method.

Return value: It returns a Boolean value indicating the existence or non-existence of the path.

For more node-related knowledge, please visit: nodejs tutorial! !

The above is the detailed content of How does the node fs module detect whether a file exists?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!