Registry / devops / recursive-copy

recursive-copy

JSON →
library2.0.14jsnpmunverified

recursive-copy is a robust and flexible utility for copying files and directories within a Node.js environment. It is currently stable at version 2.0.14 and appears to be actively maintained, with a focus on reliability and advanced use cases for filesystem operations. Key features include recursive directory copying, sophisticated filtering using functions, regular expressions, or globs, dynamic file renaming, and stream-based content transformation. It differentiates itself by integrating with `graceful-fs` and `mkdirp` to handle common filesystem errors, automatically filters out OS junk files by default, and provides an event-driven interface alongside traditional callback and promise-based APIs. This makes it suitable for complex build processes or file manipulation tasks where fine-grained control and error handling are crucial, offering a more feature-rich alternative to basic `fs.copyFile` or `fs.cp` methods.

npm install recursive-copy
INSTALL
IMPORT
SIG · RECURSIVE-COPY
R
recursive-copy
devopsjavascriptv2.0.14
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

copy
import copy from 'recursive-copy';
import { copy } from 'recursive-copy';
The primary export is a default export, even though the variable name 'copy' is common. For CommonJS, use `const copy = require('recursive-copy');`.
copy.events
import copy from 'recursive-copy'; const { COPY_FILE_START, ERROR } = copy.events;
import { events } from 'recursive-copy';
Events are exposed as properties on the default `copy` function, not as a separate named export. Access them via `copy.events`.
Options
import copy, { type Options } from 'recursive-copy';
For TypeScript users, the `Options` interface can be imported as a named type for stricter type checking when configuring copy operations.

This quickstart demonstrates how to recursively copy a directory, including dotfiles, with specific filters, using the promise-based `async/await` syntax. It also includes cleanup.

import copy from 'recursive-copy'; import path from 'path'; import fs from 'fs'; const sourceDir = path.join(process.cwd(), 'temp_src'); const destDir = path.join(process.cwd(), 'temp_dest'); // Create dummy source files for demonstration fs.mkdirSync(sourceDir, { recursive: true }); fs.writeFileSync(path.join(sourceDir, 'file1.txt'), 'Hello world!'); fs.writeFileSync(path.join(sourceDir, '.dotfile'), 'Hidden content.'); fs.mkdirSync(path.join(sourceDir, 'subdir'), { recursive: true }); fs.writeFileSync(path.join(sourceDir, 'subdir', 'file2.js'), 'console.log("JS file");'); async function runCopyExample() { try { console.log(`Copying from ${sourceDir} to ${destDir}...`); const results = await copy(sourceDir, destDir, { overwrite: true, // Overwrite if destination files exist dot: true, // Copy dotfiles filter: ['**/*', '!*.log'] // Copy all files except .log files }); console.info(`Copied ${results.length} files successfully.`); results.forEach(op => console.log(` - ${op.src} -> ${op.dest}`)); } catch (error) { console.error('Copy failed: ' + error.message); } finally { // Clean up temporary directories fs.rmSync(sourceDir, { recursive: true, force: true }); fs.rmSync(destDir, { recursive: true, force: true }); console.log('Temporary directories cleaned up.'); } } runCopyExample();
Debug
Known issues
gotchaBy default, `recursive-copy` will NOT overwrite existing files in the destination. If a file with the same name exists at the destination, the copy operation for that specific file will be skipped without an error, unless `overwrite: true` is specified.
fix
Set the `overwrite: true` option in the `options` object when calling `copy()` if existing files should be replaced.
affects: >=2.0.0
gotchaFiles starting with a dot (e.g., `.env`, `.gitkeep`, `.DS_Store`) are not copied by default. This can lead to unexpected omissions if not explicitly configured.
fix
To include dotfiles in the copy operation, set the `dot: true` option in the `options` object.
affects: >=2.0.0
gotchaSymbolic links are copied as symbolic links by default (`expand: false`). If you intend to copy the *content* of the files or directories that symlinks point to, this behavior must be changed.
fix
Set `expand: true` in the `options` object to make `recursive-copy` resolve and copy the actual content pointed to by symbolic links, rather than the links themselves.
affects: >=2.0.0
gotchaIncorrectly configured `filter` options (regex, glob, or function) can lead to files being unexpectedly included or excluded. Glob patterns are relative to the `src` directory, which can be a common source of error.
fix
Thoroughly test your `filter` options on a small dataset. For glob patterns, ensure they correctly reflect paths relative to your `src` directory. Consider using a `filter` function for complex logic to gain explicit control and debuggability.
affects: >=2.0.0
gotchaFile system permissions errors (EPERM) are common when the Node.js process lacks the necessary write privileges for the destination directory, or read privileges for the source. While `graceful-fs` helps, it cannot circumvent OS-level permission restrictions.
fix
Ensure the Node.js process has appropriate read permissions for the source path and write permissions for the destination path. This may involve running the application with elevated privileges or adjusting directory permissions on the operating system.
affects: >=2.0.0
Errors
Common errors & fixes
EPERM: operation not permitted, open 'destination/path/file.txt'
The Node.js process does not have sufficient write permissions to create or modify files in the specified destination directory.
fix
Ensure the process has write access to the `dest` directory and its parent folders. This might require changing directory permissions (`chmod`) or running the application with administrative privileges.
ENOENT: no such file or directory, stat 'source/path/non-existent-file.txt'
The source path (`src`) provided to `copy()` does not exist or is inaccessible.
fix
Verify that the `src` argument points to an actual, existing file or directory on the file system and that the Node.js process has read access to it.
TypeError: Expected a 'Buffer' or 'string', but got undefined
This error typically occurs within a custom `transform` stream if it attempts to pass an invalid or `undefined` chunk to the next stream stage, or doesn't properly handle `null` (end-of-stream) chunks.
fix
Review your `transform` function. Ensure it always emits `Buffer` or `string` data for file content, and correctly handles `done(null, chunk)` or `done(null, null)` for the end of a stream.
Upgrade
Version history
2.0.14latest on npm
Audit
Dependencies
junkrequiredUsed internally to filter out OS-specific junk files (e.g., .DS_Store, Thumbs.db) by default.
graceful-fsrequiredEmployed to enhance file system robustness and handle common errors gracefully, especially on busy systems.
mkdirprequiredUsed for creating destination directories recursively, ensuring parent directories exist before files are copied.
through2requiredA dependency for stream-based transformations, allowing for efficient content manipulation during copy.
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
recursive-copy — npm install recursive-copy · libregistry