typicode / lowdb

Simple and fast JSON database
MIT License
21.56k stars 924 forks source link

[Error: ENOENT: no such file or directory, rename '.database.json.tmp' -> 'database.json'] #555

Closed xbal023 closed 1 year ago

xbal023 commented 2 years ago

How to fix this ? [Error: ENOENT: no such file or directory, rename '.database.json.tmp' -> 'database.json'] { errno: -2, code: 'ENOENT', syscall: 'rename', path: '.database.json.tmp', dest: 'database.json' } unhandledRejection [Error: ENOENT: no such file or directory, rename '.database.json.tmp' -> 'database.json'] { errno: -2, code: 'ENOENT', syscall: 'rename', path: '.database.json.tmp', dest: 'database.json' } unhandledRejection [Error: ENOENT: no such file or directory, rename '.database.json.tmp' -> 'database.json'] { errno: -2, code: 'ENOENT', syscall: 'rename', path: '.database.json.tmp', dest: 'database.json' } unhandledRejection [Error: ENOENT: no such file or directory, rename '.database.json.tmp' -> 'database.json'] { errno: -2, code: 'ENOENT', syscall: 'rename', path: '.database.json.tmp', dest: 'database.json' } unhandledRejection

typicode commented 2 years ago

I don't know, sorry. Are there two processes trying to write to database.json? If that can help, this is the bit of code responsible for the rename call: https://github.com/typicode/steno/blob/main/src/index.ts#L44-L45

Data is written to a tmp file and when it's finished, tmp file is renamed. This is done to avoid data corruption of main db.json.

xbal023 commented 2 years ago

Saya tidak tahu, maaf. Apakah ada dua proses yang mencoba menulis ke database.json? Jika itu dapat membantu, ini adalah sedikit kode yang bertanggung jawab untuk panggilan ganti nama: https://github.com/typicode/steno/blob/main/src/index.ts#L44-L45

Data ditulis ke file tmp dan setelah selesai, file tmp diganti namanya. Ini dilakukan untuk menghindari kerusakan data pada db.json utama.

Please help my code, do I have an error here in exposing my code? const serve = {} const db = new JSONFile('database.json') serve.db = new Low(db); serve.loadDB = async function loadDB() { if (serve.db.READ) return new Promise((resolve) => setInterval(function () { (!serve.db.READ ? (clearInterval(this), resolve(serve.db.data == null ? serve.loadDB(): serve.db.data)): null) }, 1 1000)) if (serve.db.data !== null) return serve.db.READ = true await serve.db.read() serve.db.READ = false serve.db.data = { user: {}, chat: {}, set: {}, / Tambahkan kreasi kamu disini */ ...(serve.db.data || {})}; serve.db.chain = $.chain(serve.db.data) } if (serve.db) setInterval(async function() { if (serve.db.data) await serve.db.write() }, q.autoload); serve.loadDB();

enricopolanski commented 2 years ago

I have a similar issue.

My code:

import { Low } from 'lowdb';
import { JSONFile } from 'lowdb/node';

const getRootDir = () =>
  join(dirname(fileURLToPath(import.meta.url)), '/../../');

const createJSONDb = <A extends unknown>(filename: string): Low<A> => {
  // File path
  const rootdir = getRootDir();
  const file = join(rootdir, filename);

  // Configure lowdb to write to JSONFile
  const adapter = new JSONFile<A>(file);
  return new Low<A>(adapter);
};

const fileName = join(
  getRootDir(),
  'foo.json'
);

const db = createJSONDb(fileName);
await db.read();
db.data ||= { posts: [] };
await db.write();

when attempting to write I get:

Error: ENOENT: no such file or directory, open '/workspace/apps/liquid-test/workspace/apps/liquid-test/.foo.json.tmp'
prabu-ramaraj commented 1 year ago

I was able to resolve this error by following https://github.com/typicode/lowdb/issues/380 and setting up chmod 777 for the folder

typicode commented 11 months ago

It may be fixed in v7