Home  >  Article  >  Web Front-end  >  Is fs a built-in module in node?

Is fs a built-in module in node?

WBOY
WBOYOriginal
2022-04-27 15:22:222197browse

In node, fs is a built-in module, which refers to the file system module and is used to read and write files; the fs module provides methods and attributes to meet user needs for file operations, including the creation of file directories, Deletion, query, reading and writing of file contents, etc., and provides both asynchronous and synchronous methods.

Is fs a built-in module in node?

The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.

Is fs a built-in module in node?

The built-in fs module of Node.js is the file system module, which is responsible for reading and writing files.

Unlike all other JavaScript modules, the fs module provides both asynchronous and synchronous methods.

fs module is a module officially provided by Node.js and used to operate files. It provides a series of methods and attributes to meet users' needs for file operations, including creation, deletion, query of file directories, and reading and writing of file contents.

Basic operations of fs

1. Reading file content

Synchronization method readFileSync()

readFileSync() can pass in two parameters, the first parameter is the path of the file to be read; the second parameter is options, the default value is null, including encoding (encoding, default is null) and flag (identification bit, the default is r), or encoding can be passed directly.

The return value is the content of the file. If there is no encoding, the returned file content is Buffer. If it is parsed according to the incoming encoding.

//使用前需导入模块
const fs = require('fs')
let data = fs.readFileSync("./1.txt", "utf8");
console.log(data);

Asynchronous method readFile()

readFile() can pass in three parameters. The first two parameters are consistent with readFileSync(), and the third parameter is a callback function for Executed after file reading is successful.

The callback function function() has two parameters, the error message err and the return data data

const fs = require('fs')
//读取文件 (文件路径,编码格式(可省略),回调函数)
fs.readFile('./1.txt','utf8',function(err,data) {
    if(err) {
        return console.log('文件读取失败' + err.message);
    }
    console.log(data);
})

2. Writing of file content

Synchronization The method writeFileSync()

writeFileSync() has three parameters: the first parameter is the path to the file; the second parameter is the data to be written; the third parameter is options, and the default value is null. There are encoding (encoding, default is utf8), flag (identification bit, default is w) and mode (permission bit, default is 0o666), and encoding can also be passed in directly.

const fs = require('fs')
let data = fs.writeFileSync("./1.txt", "Hello World!");

Asynchronous method writeFile()

writeFile() has an additional callback function based on writeFileSync().

fs.writeFile('./1.txt','你好!',function(err) {
    if(err) {
        return console.log('文件写入失败' + err.message);
    }
    console.log('文件写入成功');
})

File writing operation, when the original file has content, the newly written content will overwrite the original content

Recommended learning: "nodejs video tutorial

The above is the detailed content of Is fs a built-in module in node?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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