Registry / http-networking / copy-paste

copy-paste

JSON →
library2.2.0jsnpmunverified

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.

npm install copy-paste
INSTALL
IMPORT
SIG · COPY-PASTE
C
copy-paste
http-networkingjavascriptv2.2.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

copy, paste
✓ const { copy, paste } = require('copy-paste');
✗ import { copy, paste } from 'copy-paste';
The core API of `copy-paste` uses CommonJS `require()` for named exports. It does not natively support ES Module `import` syntax.
clipboard (promise API)
✓ const clipboard = require('copy-paste/promises');
✗ import clipboard from 'copy-paste/promises';
The promise-based API is exposed via a separate CommonJS submodule at `copy-paste/promises`. Destructuring is also common: `const { copy, paste } = require('copy-paste/promises');`.
global()
✓ require('copy-paste').global();
This method adds `copy` and `paste` functions to the global namespace. While functional, it's generally discouraged in modern Node.js development in favor of explicit module imports.

This quickstart demonstrates the asynchronous copy and paste operations using the promise-based API, including copying plain text and stringified JSON, then pasting and parsing the content. It also includes basic error handling for missing system dependencies.

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();
Debug
Known issues
gotchaThis 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.
fix
Ensure the required system clipboard utility is installed and available in your system's PATH. For Linux, `xclip` is commonly needed (e.g., `sudo apt-get install xclip` or `sudo yum install xclip`).
affects: >=1.0.0
breakingThe `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`.
fix
Use `const clipboard = require('copy-paste');` (or `require('copy-paste/promises')`) to import the package. If your project is pure ESM, you might need to use dynamic `import()` for CommonJS modules or transpile your code.
affects: >=1.0.0
gotchaUsing `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.
fix
Prefer importing `copy` and `paste` functions directly using `const { copy, paste } = require('copy-paste');` to maintain module scope and avoid global side effects.
affects: >=1.0.0
gotchaThe `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.
fix
When retrieving content copied with `copy.json`, manually parse the result using `JSON.parse(text)` after pasting if you need to reconstitute the original object.
affects: >=1.0.0
Errors
Common errors & fixes
Error: 'xclip' command not found. Please install 'xclip' or 'xsel' to enable clipboard support.
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.
fix
Install 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'
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.
fix
Use CommonJS `require()` syntax instead: `const { copy, paste } = require('copy-paste');` or `const clipboard = require('copy-paste/promises');`.
TypeError: copy is not a function
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.
fix
Ensure you correctly import the functions before use, e.g., `const { copy, paste } = require('copy-paste');` or `const clipboard = require('copy-paste/promises');`.
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
30 hits · last 30 days
node
28
OpenAI (training)
1
Resources
copy-paste — npm install copy-paste · libregistry