reCAPTCHA WAF Session Token
Programming Languages

How to use the File System in Node.js — SitePoint

Web applications don’t always need to write to the file system, but Node.js provides a comprehensive application programming interface (API) for doing so. It may be essential if you’re outputting debugging logs, transferring files to or from a server, or creating command line tools.

Reading and writing files from code is not necessarily difficult, but your application will be more robust if you do the following:

  1. Ensure it’s cross-platform

    Windows, macOS, and Linux handle files differently. For example, you use a forward slash / to separate directories in macOS and Linux, but Windows uses a backslash \ and bans certain file name characters such as : and ?.

  2. Double-check everything!

    Users or other apps could delete a file or change access permissions. Always check for such issues and handle errors effectively.

Table of Contents

The Node.js fs Module

The Node.js fs module provides methods for managing files and directories. If you’re using other JavaScript runtimes:

All JavaScript runtimes run on a single processing thread. The underlying operating system handles operations such as file reading and writing, so the JavaScript program continues to run in parallel. The OS then alerts the runtime when a file operation is complete.

The fs documentation provides a long list of functions, but there are three general types with similar functions, which we’ll look at next.

1. Callback functions

These functions take a completion callback function as an argument. The following example passes an inline function which outputs the content of myfile.txt. Assuming no errors, its content displays after end of program appears in the console:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> readFile <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs'</span><span class="token punctuation">;</span>

<span class="token function">readFile</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> encoding<span class="token operator">:</span> <span class="token string">'utf8'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> content</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">'end of program'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

Note: the { encoding: 'utf8' } parameter ensures Node.js returns a string of text content rather than a Buffer object of binary data.

This becomes complicated when you need to run one after another and descend into nested callback hell! It’s also easy to write callback functions which look correct but cause memory leaks that are difficult to debug.

In most cases, there’s little reason to use callbacks today. Few of the examples below use them.

2. Synchronous functions

The “Sync” functions effectively ignore Node’s non-blocking I/O and provide synchronous APIs like you’d find in other programming languages. The following example outputs the content of myfile.txt before end of program appears in the console:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> readFileSync <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs'</span><span class="token punctuation">;</span>

<span class="token keyword control-flow">try</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> content <span class="token operator">=</span> <span class="token function">readFileSync</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> encoding<span class="token operator">:</span> <span class="token string">'utf8'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword control-flow">catch</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">'end of program'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

It looks easier, and I’d never say don’t use Sync … but, erm … don’t use Sync! It halts the event loop and pauses your application. That may be fine in a CLI program when loading a small initialization file, but consider a Node.js web application with 100 concurrent users. If one user requests a file which takes one second to load, they wait one second for a response — and so do all the other 99 users!

There’s no reason to use synchronous methods when we have promise functions.

3. Promise functions

ES6/2015 introduced promises. They’re syntactical sugar on callbacks to provide a sweeter, easier syntax, especially when used with async/await. Node.js also introduced a ‘fs/promises’ API which looks and behaves in a similar way to the synchronous function syntax but remains asynchronous:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> readFile <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword control-flow">try</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> content <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">readFile</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> encoding<span class="token operator">:</span> <span class="token string">'utf8'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword control-flow">catch</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">'end of program'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

Note the use of the 'node:fs/promises' module and the await before readFile().

Most examples below use the promise-based syntax. Most do not include try and catch for brevity, but you should add those blocks to handle errors.

ES module syntax

The examples in this tutorial also use ES Modules (ESM) import rather than the CommonJS require. ESM is the standard module syntax supported by Deno, Bun, and browser runtimes.

To use ESM in Node.js, either:

  • name your JavaScript files with a .mjs extension
  • use an --import=module switch on the command line — such as node --import=module index.js, or
  • if you have a project package.json file, add a new "type": "module" setting

You can still use CommonJS require should you need to.

Reading Files

There are several functions for reading files, but the simplest is to read a whole file into memory using readFile, as we saw in the example above:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> readFile <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> content <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">readFile</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> encoding<span class="token operator">:</span> <span class="token string">'utf8'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

The second options object can also be a string. It defines the encoding: set 'utf8' or another text format to read the file content into a string.

Alternatively, you can read lines one at a time using the readLines() method of the filehandle object:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> open <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> file <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword control-flow">for</span> <span class="token keyword control-flow">await</span> <span class="token punctuation">(</span><span class="token keyword">const</span> line <span class="token keyword">of</span> file<span class="token punctuation">.</span><span class="token method function property-access">readLines</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code>

There are also more advanced options for reading streams or any number of bytes from a file.

Handling File and Directory Paths

You’ll often want to access files at specific absolute paths or paths relative to the Node application’s working directory. The node:path module provides cross-platform methods to resolve paths on all operating systems.

The path.sep property returns the directory separator symbol — \ on Windows or / on Linux or macOS:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token operator">*</span> <span class="token keyword module">as</span> path</span> <span class="token keyword module">from</span> <span class="token string">'node:path'</span><span class="token punctuation">;</span>

<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token property-access">sep</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

But there are more useful properties and functions. join([…paths]) joins all path segments and normalizes for the OS:

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token method function property-access">join</span><span class="token punctuation">(</span><span class="token string">'/project'</span><span class="token punctuation">,</span> <span class="token string">'node/example1'</span><span class="token punctuation">,</span> <span class="token string">'../example2'</span><span class="token punctuation">,</span> <span class="token string">'myfile.txt'</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

resolve([…paths]) is similar but returns the full absolute path:

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token method function property-access">resolve</span><span class="token punctuation">(</span><span class="token string">'/project'</span><span class="token punctuation">,</span> <span class="token string">'node/example1'</span><span class="token punctuation">,</span> <span class="token string">'../example2'</span><span class="token punctuation">,</span> <span class="token string">'myfile.txt'</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

normalize(path) resolves all directory .. and . references:

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token method function property-access">normalize</span><span class="token punctuation">(</span><span class="token string">'/project/node/example1/../example2/myfile.txt'</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

relative(from, to) calculates the relative path between two absolute or relative paths (based on Node’s working directory):

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token method function property-access">relative</span><span class="token punctuation">(</span><span class="token string">'/project/node/example1'</span><span class="token punctuation">,</span> <span class="token string">'/project/node/example2'</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

format(object) builds a full path from an object of constituent parts:

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>
  path<span class="token punctuation">.</span><span class="token method function property-access">format</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    dir<span class="token operator">:</span> <span class="token string">'/project/node/example2'</span><span class="token punctuation">,</span>
    name<span class="token operator">:</span> <span class="token string">'myfile'</span><span class="token punctuation">,</span>
    ext<span class="token operator">:</span> <span class="token string">'txt'</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

parse(path) does the opposite and returns an object describing a path:

<code class="javascript language-javascript"><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span> path<span class="token punctuation">.</span><span class="token method function property-access">parse</span><span class="token punctuation">(</span><span class="token string">'/project/node/example2/myfile.txt'</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

Getting File and Directory Information

You often need to get information about a path. Is it a file? Is it a directory? When was it created? When was it last modified? Can you read it? Can you append data to it?

The stat(path) function returns a Stats object containing information about a file or directory object:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> stat <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> info <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">stat</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

It also provides useful methods, including:

<code class="javascript language-javascript"><span class="token keyword">const</span> isFile <span class="token operator">=</span> info<span class="token punctuation">.</span><span class="token method function property-access">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">const</span> isDirectory <span class="token operator">=</span> info<span class="token punctuation">.</span><span class="token method function property-access">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code>

The access(path) function tests whether a file can be accessed using a specific mode set via a constant. If the accessibility check is successful, the promise fulfills with no value. A failure rejects the promise. For example:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> access<span class="token punctuation">,</span> constants <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> info <span class="token operator">=</span> <span class="token punctuation">{</span>
  canRead<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  canWrite<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  canExec<span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token keyword control-flow">try</span> <span class="token punctuation">{</span>
  <span class="token keyword control-flow">await</span> <span class="token function">access</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> constants<span class="token punctuation">.</span><span class="token constant">R_OK</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  info<span class="token punctuation">.</span><span class="token property-access">canRead</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword control-flow">catch</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>


<span class="token keyword control-flow">try</span> <span class="token punctuation">{</span>
  <span class="token keyword control-flow">await</span> <span class="token function">access</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> constants<span class="token punctuation">.</span><span class="token constant">W_OK</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  info<span class="token punctuation">.</span><span class="token property-access">canWrite</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword control-flow">catch</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code>

You can test more than one mode, such as whether a file is both readable and writeable:

<code class="javascript language-javascript"><span class="token keyword control-flow">await</span> <span class="token function">access</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> constants<span class="token punctuation">.</span><span class="token constant">R_OK</span> <span class="token operator">|</span> constants<span class="token punctuation">.</span><span class="token constant">W_OK</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

Writing Files

writeFile() is the simplest function to asynchronously write a whole file replacing its content if it already exists:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> writeFile <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>
<span class="token keyword control-flow">await</span> <span class="token function">writeFile</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token string">'new file contents'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

Pass the following arguments:

  1. the file path
  2. the file content — can be a String, Buffer, TypedArray, DataView, Iterable, or Stream
  3. an optional third argument can be a string representing the encoding (such as 'utf8') or an object with properties such as encoding and signal to abort the promise.

A similar appendFile() function adds new content to the end of the current file, creating that file if it does not exist.

For the adventurous, there is a file handler write() method which allows you to replace content inside a file at a specific point and length.

Creating Directories

The mkdir() function can create full directory structures by passing an absolute or relative path:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> mkdir <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword control-flow">await</span> <span class="token function">mkdir</span><span class="token punctuation">(</span><span class="token string">'./subdir/temp'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> recursive<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

You can pass two arguments:

  1. the directory path, and
  2. an optional object with a recursive Boolean and mode string or integer

Setting recursive to true creates the whole directory structure. The example above creates subdir in the current working directory and temp as a subdirectory of that. If recursive were false (the default) the promise would reject if subdir were not already defined.

The mode is the macOS/Linux user, group, and others permission with a default of 0x777. This is not supported on Windows and ignored.

The similar .mkdtemp() function is similar and creates a unique directory typically for temporary data storage.

Reading Directory Contents

.readdir() reads the content of a directory. The promise fulfills with an array containing all file and directory names (except for . and ..). The name is relative to the directory and does not include the full path:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> readdir <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> files <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">readdir</span><span class="token punctuation">(</span><span class="token string">'./'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword control-flow">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> file <span class="token keyword">of</span> files<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


</code>

You can pass an optional second parameter object with the following properties:

  • encoding — the default is an array of utf8 strings
  • recursive — set true to recursively fetch all files from all subdirectories. The file name will include the subdirectory name(s). Older editions of Node.js may not provide this option.
  • withFileType — set true to return an array of fs.Dirent objects which includes properties and methods including .name, .path, .isFile(), .isDirectory() and more.

The alternative .opendir() function allows you to asynchronously open a directory for iterative scanning:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> opendir <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> dir <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">opendir</span><span class="token punctuation">(</span><span class="token string">'./'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword control-flow">for</span> <span class="token keyword control-flow">await</span> <span class="token punctuation">(</span><span class="token keyword">const</span> entry <span class="token keyword">of</span> dir<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>entry<span class="token punctuation">.</span><span class="token property-access">name</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code>

Deleting Files and Directories

The .rm() function removes a file or directory at a specified path:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> rm <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs/promises'</span><span class="token punctuation">;</span>

<span class="token keyword control-flow">await</span> <span class="token function">rm</span><span class="token punctuation">(</span><span class="token string">'./oldfile.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code>

You can pass an optional second parameter object with the following properties:

  • force — set true to not raise an error when the path does not exist
  • recursive — set true to recursively delete a directory and contents
  • maxRetries — make a number of retries when another process has locked a file
  • retryDelay — the number of milliseconds between retries

The similar .rmdir() function only deletes directories (you can’t pass a file path). Similarly, .unlink() only deletes files or symbolic links (you can’t pass a directory path).

Other File System Functions

The examples above illustrate the most-used options for reading, writing, updating, and deleting files and directories. Node.js also provides further, lesser-used options such as copying, renaming, changing ownership, changing permissions, changing date properties, creating symbolic links, and watching for file changes.

It may be preferable to use the callback-based API when watching for file changes, because it’s less code, easier to use, and can’t halt other processes:

<code class="javascript language-javascript"><span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> watch <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">'node:fs'</span><span class="token punctuation">;</span>


<span class="token function">watch</span><span class="token punctuation">(</span><span class="token string">'./mydir'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> recursive<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> file</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>

  <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">event: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> event <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">file changed: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> file <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


</code>

The event parameter received by the callback is either 'change' or 'rename'.

Summary

Node.js provides a flexible and cross-platform API to manage files and directories on any operating system where you can use the runtime. With a little care, you can write robust and portable JavaScript code that can interact with any file system.

For more information, refer to the Node.js fs and path documentation. Other useful libraries include:

  • OS to query operating oystem information
  • URL to parse a URL, perhaps when mapping to and from file system paths
  • Stream for handling large files
  • Buffer and TypedArray objects to handle binary data
  • Child processes to spawn a subprocess to handle long-running or complex file manipulation functions.

You can also find higher-level file system modules on npm, but there’s no better experience than writing your own.

FAQs on Accessing the File System in Node.js

What is the File System module in Node.js?

The File System module, often referred to as fs, is a core module in Node.js that provides methods and functionality for interacting with the file system, including reading and writing files.

How can I include the fs module in a Node.js script?

You can include the fs module by using the require statement, like this: const fs = require('fs');. This makes all fs methods available in your script.

What is the difference between synchronous and asynchronous file operations in Node.js?

Synchronous file operations block the Node.js event loop until the operation is completed, while asynchronous operations do not block the event loop, allowing your application to remain responsive. Asynchronous operations are typically recommended for I/O tasks.

How do I read the contents of a file in Node.js using the fs module?

You can use the fs.readFile() method to read the contents of a file. Provide the file path and a callback function to handle the data once it’s read.

What is the purpose of the callback function when working with the fs module in Node.js?

Callback functions in fs operations are used to handle the result of asynchronous file operations. They are called when the operation is complete, passing any errors and data as arguments.

How can I check if a file exists in Node.js using the fs module?

You can use the fs.existsSync() method to check if a file exists at a specified path. It returns true if the file exists and false if it doesn’t.

What is the fs.createReadStream() method, and when is it useful?

fs.createReadStream() is used for reading large files efficiently. It creates a readable stream for the specified file, allowing you to read and process data in smaller, manageable chunks.

Can I use the fs module to create and write to a new file in Node.js?

Yes, you can use the fs.writeFile() or fs.createWriteStream() methods to create and write to a new file. These methods allow you to specify the file path, content, and options for writing.

How do I handle errors when working with the fs module in Node.js?

You should always handle errors by checking the error parameter in the callback function provided to asynchronous fs methods or by using try/catch blocks for synchronous operations.

Is it possible to delete a file using the fs module in Node.js?

Yes, you can use the fs.unlink() method to delete a file. Provide the file path and a callback function to handle the result.

Can I use the fs module to work with directories and folder structures in Node.js?

Yes, the fs module provides methods to create, read, and manipulate directories, including creating directories, listing their contents, and removing directories.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock