Node.js copy file EPERM: operation not permitted
Asked Answered
E

2

20

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 ?

Evaleen answered 26/6, 2021 at 15:47 Comment(2)
When you copy the file using prompt, did you run the prompt as administrator ?Spermatophyte
@ĐăngKhoaĐinh I use Hyper as prompt but with current user (no administrator )Evaleen
E
13

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 ");
 });
Evaleen answered 30/10, 2022 at 7:52 Comment(1)
Permission error on a malformed destination path is kind of evil, especially when it's legitimate to think that the destination folder should be enough (as the file could keep it's old name). Anyways, thanks !Swithbert
V
0

This is how to recursively copy files from one directory to another

import { copyFileSync, mkdirSync, readdirSync, statSync } from 'fs';
import { join } from 'path';

const SOURCE_PATH = `libs/models/src/lib/template/sample`;
const TARGET_PATH = SOURCE_PATH.replace('sample', 'product');
const DIRS = readdirSync(SOURCE_PATH);

function isFile(target: string) {
  return statSync(target).isFile();
}

function copyFile(source: string, target: string) {
  copyFileSync(source, target);
  return;
}

function copyFiles(source: string, target: string) {
  const dirs = readdirSync(source);
  for (const d of dirs) {
    if (isFile(join(source, d))) {
      copyFile(join(source, d), join(target, d));
    } else {
      mkdirSync(join(target, d));
      copyFiles(join(source, d), join(target, d));
    }
  }
}

copyFiles(SOURCE_PATH, TARGET_PATH);

Villalba answered 7/8, 2023 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.