So I've seen several questions similar to this issue but I haven't found anything that quite fixes it.
I'm able to rename a directory with node.js as long as the directory is empty. Once I add contents, I get the error: EPERM: operation not permitted. If I delete the contents, I regain the ability to rename. Is there something simple I'm missing here? Nothing crazy in the code... just can't rename a directory if it has contents.
app.post('/rename', function(req, res, next) {
let _target = req.body.target,
_from = req.body.from,
_to = req.body.to;
fs.renameSync(
'public/' + _target + '/' + _from,
'public/' + _target + '/' + _to,
function(err, data) {
if (err) { sLog(err); return }
res.send('File Renamed');
}
);
});
-- EDIT --
The issue is when the directory has sub directories. Files in the directory to rename don't seem to affect the action
The issue is that I was using nodemon instead of node. Apparently, as a listener, it was locking the files... once I ran everything as node, it functioned as expected.
fs.renameSync doesn't work when the folder is not empty, so you should use another way just like the following:
import fsExtra from "fs-extra";
//then copy and delete, disgusting but work
fsExtra.copySync(oldpath, path);
fsExtra.rmdirSync(oldpath, { recursive: true });