You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fs

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs

0.0.2
Source
npmnpm
Version published
Weekly downloads
1.9M
-3.07%
Maintainers
1
Weekly downloads
 
Created

What is fs?

The 'fs' package in Node.js provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions. It allows you to perform operations such as reading, writing, updating, and deleting files and directories.

What are fs's main functionalities?

Reading Files

This feature allows you to read the contents of a file asynchronously. The 'readFile' method takes the file path, encoding, and a callback function to handle the file data or error.

const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Writing Files

This feature allows you to write data to a file asynchronously. The 'writeFile' method takes the file path, data to write, and a callback function to handle any errors.

const fs = require('fs');
fs.writeFile('example.txt', 'Hello, world!', (err) => {
  if (err) throw err;
  console.log('File has been saved!');
});

Updating Files

This feature allows you to append data to an existing file. The 'appendFile' method is used to add data to the end of a file asynchronously.

const fs = require('fs');
fs.appendFile('example.txt', ' More data.', (err) => {
  if (err) throw err;
  console.log('Data has been appended!');
});

Deleting Files

This feature allows you to delete a file. The 'unlink' method is used to remove a file asynchronously.

const fs = require('fs');
fs.unlink('example.txt', (err) => {
  if (err) throw err;
  console.log('File has been deleted!');
});

Creating Directories

This feature allows you to create a new directory. The 'mkdir' method can create a directory and, with the 'recursive' option, can create nested directories.

const fs = require('fs');
fs.mkdir('newDir', { recursive: true }, (err) => {
  if (err) throw err;
  console.log('Directory created!');
});

Other packages similar to fs

FAQs

Package last updated on 12 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts