# cpx2

> Copy file globs, watching for changes.

Latest version **9.0.3** (published 2026-09-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install cpx2
pnpm add cpx2
yarn add cpx2
bun add cpx2
```

Provides the command `cpx`.

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 9.0.3 |
| Published | 2026-09-09 |
| First published | 2019-08-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^22.0.0 \|\| >=24.0.0 |
| Dependencies | 8 |
| Unpacked size | 71.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 62 |
| Author | Toru Nagashima |
| Maintainers | bret |
| Keywords | cp, cli, tool, commandline, sync, rsync, watch, observe, copy, dir, directory, directories, file, files |

## Links

- npm: https://www.npmjs.com/package/cpx2
- Repository: https://github.com/bcomnes/cpx2
- Issues: https://github.com/bcomnes/cpx2/issues
- npm.io page: https://npm.io/package/cpx2

## Dependencies (8)

- [glob](https://npm.io/package/glob.md) ^13.0.0
- [p-map](https://npm.io/package/p-map.md) ^7.0.0
- [ignore](https://npm.io/package/ignore.md) ^7.0.5
- [subarg](https://npm.io/package/subarg.md) ^1.0.0
- [debounce](https://npm.io/package/debounce.md) ^3.0.0
- [glob2base](https://npm.io/package/glob2base.md) 0.0.12
- [minimatch](https://npm.io/package/minimatch.md) ^10.0.1
- [shell-quote](https://npm.io/package/shell-quote.md) ^1.8.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 9.0.3 (latest) — 2026-09-09
- 9.0.2 — 2026-09-08
- 9.0.1 — 2026-09-07
- 9.0.0 — 2026-05-19
- 8.0.2 — 2026-04-07
- 8.0.1 — 2026-04-03
- 8.0.0 — 2024-10-04
- 7.0.2 — 2024-10-04
- 7.0.1 — 2023-12-29
- 7.0.0 — 2023-12-25
- 6.0.1 — 2023-10-25
- 6.0.0 — 2023-10-23
- 5.0.0 — 2023-07-05
- 4.2.3 — 2023-04-03
- 4.2.2 — 2023-03-07
- … 10 more at https://npm.io/package/cpx2/versions

## README

# cpx2

[![npm version](https://img.shields.io/npm/v/cpx2.svg)](https://www.npmjs.com/package/cpx2)
[![Downloads/month](https://img.shields.io/npm/dm/cpx2.svg)](http://www.npmtrends.com/cpx2)
[![tests](https://github.com/bcomnes/cpx2/actions/workflows/test.yml/badge.svg)](https://github.com/bcomnes/cpx2/actions/workflows/test.yml)

Copy file globs, watching for changes.

This module provides a CLI tool like `cp`, but with watching.

This is a maintained fork of [mysticatea/cpx](https://github.com/mysticatea/cpx).  It retains the `cpx` bin name, so it can act as a drop-in replacement.

## Installation

```
npm install cpx2
```

## Usage

```
Usage: cpx <source> <dest> [options]

    Copy files, watching for changes.

        <source>  The glob of target files.
        <dest>    The path of a destination directory.

Options:

    -c, --command <command>   A command text to transform each file.
    -C, --clean               Clean files that matches <source> like pattern in
                              <dest> directory before the first copying.
    -f, --force               Force the file to be copied, even if the
                              destination is readonly.   
    -i, --ignore              A comma separated list of gitignore style ignore
                              patterns.
    -L, --dereference         Follow symbolic links when copying from them.
    -h, --help                Print usage information.
    --include-empty-dirs      The flag to copy empty directories which is
                              matched with the glob.
    --no-initial              The flag to not copy at the initial time of watch.
                              Use together '--watch' option.
    -p, --preserve            The flag to copy attributes of files.
                              This attributes are uid, gid, atime, and mtime.
    -t, --transform <name>    A module name to transform each file. cpx lookups
                                the specified name via "require()".
    -u, --update              The flag to not overwrite files on destination if
                              the source file is older.
    -v, --verbose             Print copied/removed files.
    -V, --version             Print the version number.
    -w, --watch               Watch for files that matches <source>, and copy
                              the file to <dest> every changing.
```


## Example

```
$ cpx "src/**/*.{html,png,jpg}" app --watch
```

This example will copy html/png/jpg files from `src` directory to `app`
directory, keeping file tree structure.
Whenever the files are changed, copy them.

> Since Bash expands globs, requires to enclose it with double quotes.

You can use together [Browserify](http://browserify.org).

```
$ cpx "src/**/*.{html,png,jpg}" app -w & watchify src/index.js -o app/index.js
```

You can use shell commands to convert each file.

```
$ cpx "src/**/*.js" app -w -c "babel --source-maps inline"
```

You can use the transform packages for Browserify.

```
$ cpx "src/**/*.js" app -w -t babelify -t uglifyify
```

It maybe can use to add header comment, to optimize images, or etc...


## Node.js API

You can use this module as a node module.

```js
import * as cpx from "cpx2";
```

### cpx.copy

```ts
cpx.copy(source, dest, options, callback)
cpx.copy(source, dest, callback)
```

- **source** `{string}` -- A file glob of copy targets.
- **dest** `{string}` -- A file path of a destination directory.
- **options** `{object}`
  - **options.clean** `{boolean}` -- The flag to remove files that copied on past before copy. Default: `false`.
  - **options.dereference** `{boolean}` -- The flag to follow symbolic links when copying from them. Default: `false`.
  - **options.includeEmptyDirs** `{boolean}` -- The flag to copy empty directories which is matched with the glob. Default: `false`.
  - **options.initialCopy** `{boolean}` -- The flag to not copy at the initial time of watch. This is for `cpx.watch()`. Default: `true`.
  - **options.force** `{boolean}` -- The flag to copy file to the destination, even if it is readonly.
  - **options.preserve** `{boolean}` -- The flag to copy uid, gid, atime, and mtime of files. Default: `false`.
  - **options.transform** `{((filepath: string) => stream.Transform)[]}` -- Functions that creates a `stream.Transform` object to transform each copying file.
  - **options.update** `{boolean}` -- The flag to not overwrite files on destination if the source file is older. Default: `false`.
  - **options.ignore** `{string|Array<string>}` -- A gitignore style string or array of strings that make ignoring directory patterns easier. Default: []
- **callback** `{(err: Error|null) => void}` -- A function that is called at done.

Copy files that matches with `source` glob to `dest` directory.

### cpx.copySync

```ts
cpx.copySync(source, dest, options)
cpx.copySync(source, dest)
```

A synchronous function of `cpx.copy`.

Arguments is almost same as `cpx.copy`.
But `options.transform` is not supported.

### cpx.watch

```ts
cpx.watch(source, dest, options)
cpx.watch(source, dest)
```

Copy files that matches with `source` glob string to `dest` directory.
After the first copy, starts observing.  And copy the files when every changes.

Arguments is same as `cpx.copy`.

`cpx.watch` returns an `EventEmitter`.

- `.on("copy", (e) => { ... })` : Be fired after file is copied. `e.srcPath` is a path of original file. `e.dstPath` is a path of new file.
- `.on("remove", (e) => { ... })` : Be fired after file is removed. `e.path` is a path of removed file.
- `.on("watch-ready", () => { ... })` : Be fired when started watching files, after the first copying.
- `.on("watch-error", (err) => { ... })` : Be fired when occured errors during watching.

## Changelog

[GitHub Releases](https://github.com/bcomnes/cpx2/releases)

## Contributing

Thank you for contributions!

### Bug Reports or Feature Requests

Please use GitHub Issues.

### Document Corrections

Please use GitHub Pull Requests.
I would especially thank for document corrections since I'm not familiar with English.

### Feature Implementing

Please use GitHub Pull Requests.

There are some npm-scripts to help developments.

- `npm test` - Run lint, dependency checks, and tests with coverage.
- `npm run clean` - Delete directories and files which are created by other commands.
- `npm run lint` - Run ESLint.
- `npm run watch` - Run tests when each file was modified.

---
_Source: https://npm.io/package/cpx2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
