tinyclip - System Clipboard Utility
tinyclip is a concise, cross-platform utility designed to interact with the system clipboard specifically within Node.js environments. It leverages native operating system clipboard functionalities for robust performance. The package is currently at version `0.1.12` and demonstrates an active development cadence, with frequent minor updates addressing features and bug fixes. A key differentiator is its minimalistic design and its exclusive focus on Node.js; starting from version `0.1.11`, `tinyclip` explicitly removed browser support, guiding developers to utilize the native browser Clipboard API for web contexts instead. This makes it an ideal choice for server-side or desktop applications built with Node.js that require reliable clipboard operations without additional heavy dependencies.
Common errors
-
ReferenceError: document is not defined
cause Attempting to use `tinyclip` in a browser environment after its browser support was removed in version `0.1.11`.fixThis library is Node.js-only since v0.1.11. For browser applications, use the native `navigator.clipboard` API. -
SyntaxError: Named export 'readText' not found. The requested module 'tinyclip' is a CommonJS module, which may not support named exports.
cause Trying to import `tinyclip` using CommonJS `require()` syntax or using named imports (`import { ... }`) in a CommonJS context without proper ESM setup.fixEnsure your Node.js project is configured for ECMAScript Modules (ESM) by setting `"type": "module"` in your `package.json` or by using `.mjs` file extensions, and use `import { readText, writeText } from 'tinyclip';`.
Warnings
- breaking Browser support was entirely removed in version `0.1.11`. Attempts to use `tinyclip` in a browser environment after this version will result in errors.
- breaking Node.js engine constraints were introduced in version `0.1.12`, requiring Node.js versions `^16.14.0 || >= 17.3.0`.
- gotcha This library is exclusively designed for Node.js environments. Although older versions might have had limited browser compatibility, it is no longer maintained nor supported for web applications.
Install
-
npm install tinyclip -
yarn add tinyclip -
pnpm add tinyclip
Imports
- readText
const { readText } = require('tinyclip')import { readText } from 'tinyclip' - writeText
const writeText = require('tinyclip').writeTextimport { writeText } from 'tinyclip'
Quickstart
import { readText, writeText } from 'tinyclip';
async function clipboardExample() {
const textToCopy = 'Hello from tinyclip! This text was written to your clipboard.';
console.log(`Attempting to write: "${textToCopy}" to clipboard.`);
try {
await writeText(textToCopy);
console.log('Text successfully written to clipboard.');
console.log('Attempting to read text from clipboard...');
const clipboardContent = await readText();
console.log(`Text read from clipboard: "${clipboardContent}"`);
if (clipboardContent === textToCopy) {
console.log('Clipboard content matches the written text.');
} else {
console.log('Clipboard content does not match the written text. (This might be due to other applications modifying the clipboard or environment limitations)');
}
} catch (error: any) {
console.error('Failed to interact with clipboard:', error.message);
console.error('Ensure you are running in a Node.js environment with appropriate OS access.');
}
}
clipboardExample();