Home > Article > Web Front-end > How does the node fs module detect whether a file exists?
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)".
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");
2. Use the fs module Detection
fs.exists(filePath, (exists) => { if (exists) { console.log("文件已存在"); } else { console.log("文件不存在"); } });
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 )
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!