Copy to clipboard in Node.js?
For OS X:
function pbcopy(data) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(data); proc.stdin.end();
}
write()
can take a buffer or a string. The default encoding for a string will be utf-8.
Check out clipboardy
. It lets you copy/paste cross-platform. It is more actively maintained than the copy-paste
module mentioned in another answer and it fixes many of that module's issues.
const clipboardy = require('clipboardy');
// Copy
clipboardy.writeSync('🦄');
// Paste
clipboardy.readSync();
//🦄
Here's a module that provide copy
and paste
functions: https://github.com/xavi-/node-copy-paste
When require("copy-paste").global()
is executed, two global functions are added:
> copy("hello") // Asynchronously adds "hello" to clipbroad
> Copy complete
> paste() // Synchronously returns clipboard contents
'hello'
Like many of the other answer mentioned, to copy and paste in node you need to call out to an external program. In the case of node-copy-paste
, it calls out to pbcopy/pbpaste
(for OSX), xclip
(for linux), and clip
(for windows).
This module was very helpful when I was doing a lot of work in the REPL for a side project. Needless to say, copy-paste
is only a command line utility -- it is not meant for server work.