Node.js API

syncDirectory()

CommonJS and ESM both work. The default export is synchronous. Use .async() when you need a Promise or async afterEachSync.

Imports

// CommonJS
const syncDirectory = require('sync-directory');
const { async } = require('sync-directory');

// ESM
import syncDirectory from 'sync-directory/index.mjs';
import { async } from 'sync-directory/index.mjs';

Which function

CallReturnsBlocks?
syncDirectory() / .sync()undefined, or a chokidar watcher if watch: trueYes
syncDirectory.async()PromiseNo

Minimal sync

syncDirectory(srcDir, targetDir, {
  afterEachSync({ eventType, nodeType, relativePath, srcPath, targetPath }) {
    // eventType: init:copy | init:hardlink | add | change | unlink | unlinkDir | addDir
  },
});

Async with a delay per file

await syncDirectory.async(srcDir, targetDir, {
  async afterEachSync() {
    await new Promise((r) => setTimeout(r, 2000));
  },
});

Options that matter

  • type: 'copy' | 'hardlink' — copy is default.
  • watch + chokidarWatchOptions — live sync.
  • skipInitialSync — watch only; skip the first full tree copy.
  • exclude / forceSync — string, RegExp, array, or function. Priority: forceSync > exclude.
  • deleteOrphaned — drop leftover (and excluded) files in target.
  • stayHardlink — keep the target as a hardlink after edits (default true when type is hardlink).
  • staySymlink — if src folder is a symlink, target is the same symlink.
  • nodeep — first level only.
  • cwd — resolve relative src/target. Default process.cwd().
  • onError — default throws.