unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

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

Open mutewinter opened this issue 4 months ago • 0 comments

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.

mutewinter avatar Oct 03 '25 12:10 mutewinter