Belphemur/node-json-db

"Error: EPERM: operation not permitted" when json-file is opened for viewing in other program

Closed this issue · 1 comments

If the database (json-file) is opened for viewing in another program (I usually view it with FAR manager) the module can't save it (it actually fails when trying to rename the file).
Sample error message:

...\Server\node_modules\node-json-db\dist\JsonDB.js:334
            throw new Errors_1.DatabaseError("Can't save the database", 2, err);
                  ^

DatabaseError: Can't save the database
    at JsonDB.save (...\Server\node_modules\node-json-db\dist\JsonDB.js:334:19)
    at async ...\Server\node_modules\node-json-db\dist\JsonDB.js:269:17
    at async lock.writeLock (...\Server\node_modules\node-json-db\dist\lock\Lock.js:50:32) {
  inner: [Error: EPERM: operation not permitted, rename 'D:\Users\???\AppData\Local\Temp\database.json.tmp-18207549785e6069' -> 'D:\Users\???\AppData\Local\Temp\database.json'] {
    errno: -4048,
    code: 'EPERM',
    syscall: 'rename',
    path: 'D:\\Users\\???\\AppData\\Local\\Temp\\database.json.tmp-18207549785e6069',
    dest: 'D:\\Users\\???\\AppData\\Local\\Temp\\database.json'
  },
  id: 2
}

Code fragments:

const http =        require("http");
const JsonDB =      require("node-json-db");
const os =          require("os");
/// ...
const db = new JsonDB.JsonDB(new JsonDB.Config(path.join(os.tmpdir(), 'database'), true, true, '/'));
// ...
const server = http.createServer(async (req, res) => {
    console.log("req.url:", req.url); // DEBUG
    await db.push(`/last_active_time`, Date.now(), false);
}
server.listen((9999), () => {
    console.log("Server is listening at port", server.address().port);
})

It seems the issue is caused by the implementation of rename syscall - it can't handle such case properly.
When I try to rename the database (while it is open in FAR manager) other way (for instance using same FAR manager), no error occurs.

OS: Windows 10 21H1
Node.js: 18.12.1
node-json-db: 2.1.3

Hello,

Unfortunately, there isn't anything that I can do for this.

I would suggest using another editor (like notepad++) that doesn't ask for exclusive access to the file (which is the default way that Windows works).