copy-paste Clipboard Utility
copy-paste is a Node.js utility designed to provide read/write access to the system clipboard across different operating systems. It achieves cross-platform compatibility by wrapping native command-line tools: `pbcopy` and `pbpaste` for macOS, `xclip` for Linux and BSD systems, and `clip` for Windows. This abstraction allows developers to interact with the clipboard using a unified JavaScript API. The package offers both a traditional callback-based interface for asynchronous operations and a modern Promise-based API for `async/await` patterns, accessible via a submodule. It is currently at version 2.2.0 and, while not actively undergoing rapid feature development, it remains a maintained package for fundamental clipboard interactions in Node.js environments. Its primary differentiator is its robust cross-platform wrapper for underlying system utilities, abstracting away the OS-specific commands.
Common errors
-
Error: 'xclip' command not found. Please install 'xclip' or 'xsel' to enable clipboard support.
cause The `copy-paste` package requires external system utilities to function. On Linux, `xclip` or `xsel` is necessary and was not found in the system's PATH.fixInstall the `xclip` utility on your Linux system. For Debian/Ubuntu-based systems: `sudo apt-get install xclip`. For RHEL/Fedora-based systems: `sudo yum install xclip`. -
SyntaxError: Named export 'copy' not found. The requested module 'copy-paste' does not provide an export named 'copy'
cause This error occurs when attempting to use ES Module `import` syntax (`import { copy } from 'copy-paste';`) with this CommonJS-only package in an ESM context.fixUse CommonJS `require()` syntax instead: `const { copy, paste } = require('copy-paste');` or `const clipboard = require('copy-paste/promises');`. -
TypeError: copy is not a function
cause The `copy` or `paste` functions were called without being properly imported or destructured from the `require('copy-paste')` object, meaning they are undefined in the current scope.fixEnsure you correctly import the functions before use, e.g., `const { copy, paste } = require('copy-paste');` or `const clipboard = require('copy-paste/promises');`.
Warnings
- gotcha This package relies on external system utilities (`pbcopy`/`pbpaste` on macOS, `xclip` on Linux/BSD, `clip` on Windows) which might not be installed by default or configured correctly on all systems. Absence of these tools will lead to runtime errors when attempting clipboard operations.
- breaking The `copy-paste` package is CommonJS-only and does not natively support ES Module `import` syntax. Attempting to `import` it directly in a pure ESM Node.js environment will result in a `SyntaxError` or `ERR_REQUIRE_ESM`.
- gotcha Using `require('copy-paste').global()` pollutes the global namespace with `copy` and `paste` functions, which is generally considered an anti-pattern in modern JavaScript development and can lead to naming conflicts.
- gotcha The `copy.json(obj)` method stringifies the provided JavaScript object to a JSON string before copying it to the clipboard. Pasting this content will yield the JSON string, not the original JavaScript object.
Install
-
npm install copy-paste -
yarn add copy-paste -
pnpm add copy-paste
Imports
- copy, paste
import { copy, paste } from 'copy-paste';const { copy, paste } = require('copy-paste'); - clipboard (promise API)
import clipboard from 'copy-paste/promises';
const clipboard = require('copy-paste/promises'); - global()
require('copy-paste').global();
Quickstart
import { setTimeout } from 'timers/promises';
// Use the promise-based API for modern async/await patterns
const { copy, paste } = require('copy-paste/promises');
async function runClipboardExample() {
try {
console.log('Copying text...');
await copy('Hello from copy-paste! ' + new Date().toISOString());
console.log('Text copied.');
await setTimeout(100);
console.log('Pasting text...');
const text = await paste();
console.log('Clipboard content:', text);
console.log('Copying JSON object...');
const data = { message: 'Clipboard data', timestamp: Date.now(), source: 'copy-paste' };
await copy.json(data);
console.log('JSON object stringified and copied.');
await setTimeout(100);
console.log('Pasting JSON string...');
const jsonText = await paste();
console.log('Clipboard JSON string:', jsonText);
const parsedData = JSON.parse(jsonText);
console.log('Parsed JSON object:', parsedData);
} catch (error) {
console.error('An error occurred:', error.message);
// Provide guidance for common errors
if (error.message.includes('command not found')) {
console.error('Hint: Ensure `xclip` (Linux), `pbcopy`/`pbpaste` (macOS), or `clip` (Windows) is installed and in your PATH.');
}
}
}
runClipboardExample();