Search code examples
node.jswindowscopyfilesystems

Node.js copy file EPERM: operation not permitted


I try to copy a font with nodejs v14.17.0 to %localappdata%\Microsoft\Windows\Fonts , when try with prompt I don't have problem

copy /B "Haloha Free Trial.ttf" /V %localappdata%\Microsoft\Windows\Fonts\
    1 file(s) copied.

but when try with nodejs, I've this issue

[Error: EPERM: operation not permitted, copyfile 'D:\dev\test\javascript\font\Haloha Free Trial.ttf' -> 'C:\Users\omen\AppData\Local\Microsoft\Windows\Fonts'] {
  errno: -4048,
  code: 'EPERM',
  syscall: 'copyfile',
  path: 'D:\\dev\\test\\javascript\\font\\Haloha Free Trial.ttf',
  dest: 'C:\\Users\\omen\\AppData\\Local\\Microsoft\\Windows\\Fonts'
}

This is my code

let options = process.argv.slice(2);
console.log(options[0]);

console.log(process.env.LOCALAPPDATA);
const locallAppdata = process.env.LOCALAPPDATA;

const fs = require('fs');

fs.copyFile( options[0], locallAppdata+'\\Microsoft\\Windows\\Fonts\\', (err) =>{
    if(err) throw err;
    console.log( argv[0] + " was copied ");
});

How to solve it ?


Solution

  • In according with official documentation fs.copyFile

    • src <string> | <Buffer> | <URL> source filename to copy
    • dest <string> | <Buffer> | <URL> destination filename of the copy operation

    In dest is required destination filename and not only destination directory.

    fs.copyFile(src, dest[, mode], callback)
    

    In this case dest = 'locallAppdata+'\\Microsoft\\Windows\\Fonts\\' + options[0]

    let dest = locallAppdata+'\\Microsoft\\Windows\\Fonts\\' + options[0];
    let src =  options[0];
        
     fs.copyFile( src, dest, (err) =>{
         if(err) throw err;
                 console.log( options[0] + " was copied ");
     });