copy-paste Clipboard Utility

2.2.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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();

view raw JSON →