File System in Node.js

File System

                   The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.

To use this module:
                   
                                const fs = require('fs')

All file system operations have synchronous and asynchronous forms.
The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.

                  const fs = require('fs');
                  fs.unlink('/tmp/hello',(err)  =>{
                  if(err) throw err;
                  console.log ('Successfully deleted /tmp/hello');

Exceptions that occur using synchronous operations are thrown immediately and may be handled using try/catch, or may be allowed to bubble up.

                       const fs = require('fs');
                   try{
                  fs.unlinksync('/tmp/hello',(err)  =>{
                  console.log ('Successfully deleted /tmp/hello');
                   }catch(err){
                   }

There is no guaranteed ordering when using asynchronous methods. So the following is prone to error because the fs.stat() operation may complete before the fs.rename() operation:

                  fs.rename('/tmp/hello', '/tmp/world',(err) => {
                  if(err) throw err;
                  console.log('rename complete');
                  });
                   fs.stat('/tmp/world', (err,stats) =>{
                   if(err)throw err;
                   console.log('stats',${JSON.stringify(stats)}');
                   });
                  
To correctly order the operations, move the fs.stat() call into the callback of the fs.rename() operation:
                   fs.rename('/tmp/hello','/tmp/world',(err) => {
                   if(err) throw err;
                   fs.stat('/tmp/world',(err,stats)=>{
                   if(err) throw err;
                   console.log('stats',${JSON.stringify(stats)}');
});
});
  In busy processes, use the asynchronous versions of these calls. The synchronous versions will block the entire process until they complete, halting all connections.
While it is not recommended, most fs functions allow the callback argument to be omitted, in which case a default callback is used that rethrows errors. To get a trace to the original call site, set the NODE_DEBUG environment variable:
Omitting the callback function on asynchronous fs functions is deprecated and may result in an error being thrown in the future.
        $cat script.js
        function bad() {
       require('fs').readFile('/');
       }
       bad();
       $ env NODE_DEBUG = fs node script.js
       fs.js:88
       throw backtrace;
     ERROR:EISDIR: illegal operation on a directory , read.
      <stack trace.>


       

                 


                 
                  


                  

Comments