![](/img/trans.png)
[英]How do I write image to a specific directory with fs module in nodejs?
[英]How to copy and write file in the same directory using fs module in nodejs?
尝试从目录复制文件并使用fs
模块在同一目录中写入新文件它似乎不起作用不确定是什么实现了错误任何帮助都会得到认可,一旦创建新文件然后我将在新的 json 文件中进行更改就是我试图实现。
主文件
const fs = require("fs");
async function copyAndCreateNewFile (path, files) {
try {
let rawdata = fs.readFileSync(files[0]);
let parsedJson = JSON.parse(rawdata);
console.log("JSON DATA", parsedJson);
let data = JSON.stringify(parsedJson);
const newFile = fs.writeFileSync(`${path}/${"test.json"}`, data);
return newFile;
}
catch(err) {
console.log ('not able to write data in the file ')
}
}
copyAndCreateNewFile('dir/services', ['dir/services/fileToCopy.json'])
错误
not able to write data in the file { Error: EACCES: permission denied, open '/test.json'
在 Node 中复制文件的更简单方法:
const fs = require('fs');
fs.copyFile('source', 'destination', (err) => {
console.error(err);
});
确保您对目标目录具有写权限。
例如:
sudo chmod 777 <destination-dir>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.