![](/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.