Table of Contents
  • File System

    published: 2026-07-31

    tags: blogs,development,javascript,node

    examples

    in the examples below

    var file // a file path as string

    date times of a file

    import fs from 'fs'
    
    const { birthtime, mtime, ctime } = fs.statSync(file)

    all values are date instances

    create an empty file

    import fs from 'fs-extra'
    
    await fs.ensureFile(path)

    Async Check File Exists

    import { promises as fsPromises } from 'fs';   
    
    async function exists (path) {  
      try {
        await fsPromises.access(path)
        return true
      } catch {
        return false
      }
    }

    Async rename file

    import { promises as fsPromises } from 'fs';   
    
    await fsPromises.rename(oldPath, newPath)  

    is path a directory|file

    import { promises as fsPromises } from 'fs';   
    
    const stats = await Fs.stat(path)
    
    console.log(stats.isDirectory())
    console.log(stats.isFile())

    home directory

    const os = require('os')
    
    os.homedir()  

    update file changed & access date times

    import fs from 'fs-extra'
    
    async touch (file) {  
      await fs.ensureFile(file)
    
      const now = new Date()
      await fs.utimes(file, now, now)
    }

    file contents

    import { promises as fsPromises } from 'fs';
    
    const contents = await fsPromises.readFile(file, 'utf8');