Skip to content

FS Driver's .watch doesn't fire unless the directory already exists #684

@mutewinter

Description

@mutewinter

Environment

Node 22 and Unstorage 1.17.1.

Reproduction

import { createStorage } from "unstorage";
import fsDriver from "unstorage/drivers/fs";

const storage = createStorage({
  driver: fsDriver({ base: "./.storage/test" }),
});

// Set up watch BEFORE the directory exists
await storage.watch((event, key) => {
  console.log("Storage changed:", event, key);
});

// watch callback will NOT fire
await storage.setItem("key1", "value1");

Describe the bug

My theory:

The fs driver's watch() method uses chokidar to monitor file system changes. When watch() is called on a path that doesn't exist yet (e.g., ./.storage/test), chokidar initializes the watcher but may not properly detect the first write operation that creates the directory structure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions