Migrating from chokidar 3.x to 4.x

James Garbutt - Sep 18 - - Dev Community

A big win recently in the e18e space is that chokidar published a new 4.x version!

This new version drops a whole bunch of dependencies and simplifies the internal logic greatly, leaning more on modern platform capabilities.

For those of you who don't know what chokidar is - it is a widely used file system watcher which provides an abstraction over the top of Node's own watch functions. More than likely, it is somewhere in your dependency tree already!

Dependency graph difference

In chokidar 3.x:

chokidar 3.x dependency graph

In chokidar 4.x:

chokidar 4.x dependency graph

Changes in 4.x

Most of the changes are internal and shouldn't affect how you use the library, but will improve performance a huge amount.

A few notable changes:

  • fsevents was dropped since Node itself handles cross-platform compatibility well these days
  • ES module support
  • Improved TypeScript types
  • Various performance improvements
  • Glob support removed

The one major change from this list that will affect users is the removal of globs.

Life without globs

In chokidar 3.x, it was possible to watch a glob. For example, we could watch src/*.ts and chokidar would expand the pattern internally to watch all TypeScript files inside src/.

In 4.x, this functionality has been removed since you can achieve the same with filters or an external glob library.

An example:

// chokidar v3
watch('src/*.ts');

// chokidar v4 (RegExp)
watch('src', {
  // any path whose end is not preceded by `.ts`
  ignored: /(?<!\.ts)$/
});

// chokidar v4 (ignore function)
watch('src', {
  ignored: (path, stats) =>
    stats?.isFile() &&
    !path.endsWith('.ts')
});

// chokidar v4 (glob)
// NOTE: this will not watch newly added files. It
// will only watch the initial set of files
import {glob} from 'tinyglobby';
watch(await glob(['src/*.ts']));
Enter fullscreen mode Exit fullscreen mode

In most cases, you can probably avoid the need for a glob library and use a filter function or RegExp instead (which will also be much faster in many cases).

Feedback

If you upgrade and have any feedback or find any bugs, we'd love to hear from you via issues.

You can also catch many of us in the e18e discord working hard on migrating popular packages from 3.x to 4.x.

. . . . . . . . . . . . .
Terabox Video Player