How to Copy to Clipboard in Node.js (Easy Way)
Last updated on August 11, 2022
To copy to clipboard in Node.js, you can use the clipboardy
package from NPM. First, install it by running the following command at the root of your project directory:
npm i clipboardy
We can use clipboardy
to read or write to the system clipboard:
import clipboardy from 'clipboardy';
async function main() {
await clipboardy.write('butter');
const text = await clipboardy.read();
console.log(text); // 'butter'
}
main();
The module can read/write synchronously as well:
import clipboardy from 'clipboardy';
clipboardy.writeSync('butter');
const text = clipboardy.readSync();
console.log(text); // butter
See also
- [Solved] Cannot find module in Node.js (MODULE_NOT_FOUND)
- How to Create a Video Element in JavaScript (Easy Way)
- req.body is undefined in Express? Here's what to do
- How to Get the First Element of a Map in JavaScript (Easy Ways)
- How to Get the Window's Width on Resize in React
- How to easily fix the "Cannot read property 'classList' of null" error in JavaScript