异常
Error: EPERM: operation not permitted, copyfile 'G:\test.txt' -> 'G:\Trash'
?[90m at Object.copyFileSync (node:fs:2800:3)?[39m
at Object.<anonymous> (D:\NodeJs\node-demo\trash.js:19:29)
?[90m at Module._compile (node:internal/modules/cjs/loader:1097:14)?[39m
?[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)?[39m
?[90m at Module.load (node:internal/modules/cjs/loader:975:32)?[39m
?[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)?[39m
?[90m at node:internal/main/run_main_module:17:47?[39m {
errno: ?[33m-4048?[39m,
syscall: ?[32m'copyfile'?[39m,
code: ?[32m'EPERM'?[39m,
path: ?[32m'G:\\test.txt'?[39m,
dest: ?[32m'G:\\Trash'?[39m
}
Node.js v17.1.0
错误代码
var fs = require('fs');
fs.copyFileSync('G:\test.txt', 'G:\Trash');
原因
我以为这个方法复制文件是复制到指定目录下,所以第二个参数写了一个目录,导致报错。
解决
第二个参数改为要复制的文件路径。
正确代码
var fs = require('fs');
fs.copyFileSync('G:\test.txt', 'G:\Trash\test.txt');