June 2025
Intermediate to advanced
837 pages
24h 50m
English
Previously, the first argument of callback functions was ignored during development. Normally this argument has the value null. However, if an error occurs, you’ll find more detailed information about it in this object. You can test the error handling when dealing with files by trying to open a file that doesn’t exist. Listing 19.10 shows how you can cause such errors and how to deal with them.
import { open } from 'fs';open('/does-not-exist.txt', 'r', (err, handle) => { if (err) throw err;});
Listing 19.10 Error While Reading a File
In this example, you can see how errors can occur in the file system module of Node.js. When you run the sample code, you’ll receive the following error message: Error: ENOENT: no such ...
Read now
Unlock full access