typicode / lowdb

Simple and fast JSON database
MIT License
21.3k stars 918 forks source link

UnhandledSchemeError: Reading from "node:fs/promises" is not handled by plugins #588

Closed MuhammadSawalhy closed 4 months ago

MuhammadSawalhy commented 4 months ago

This error appeared for the first time to me while developing a Next.js@14 application. I suspected that it is due to experimental turbo bundler but after disabling it the same problem happened.

 ⨯ node:fs/promises
Module build failed: UnhandledSchemeError: Reading from "node:fs/promises" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
Import trace for requested module:
node:fs/promises
./node_modules/lowdb/lib/adapters/node/TextFile.js
./node_modules/lowdb/lib/node.js
./lib/db.ts
./actions/users.ts
./auth.ts

My environment:

Additional notes

I included import "server-only"; at the top of ./lib/db.ts file to make sure that it runs in the server. And ./actions/users.ts has "use server"; directive at the top.

MuhammadSawalhy commented 4 months ago

I also read esm-package.md and couldn't resolve the issue after adding "type": "module" in package.json.

MuhammadSawalhy commented 4 months ago

OK, sorry for the inconvenience! This happens because I used indirectly the fs module in Node.js in Edge runtime of Next.js exactly in middleware.ts. Edge runtime doesn't support many of the Node modules and has some limitations to achieve other goals like performance and low latency.

I hope this comment will help someone in the future.