Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Cant resolve 'fs'

default discord avatar
roxxellast year
4

Code:


import { zValidator } from "@hono/zod-validator";
import { getPayloadHMR } from "@payloadcms/next/utilities";
import { Hono } from "hono";
import { z } from "zod";
import configPromise from "@payload-config";

const app = new Hono().get(
    "/search",
    zValidator(
        "query",
        z.object({
            q: z.string().optional(),
        }),
    ),
    async (c) => {
        const query = c.req.query("q");
        if (!query || query.length < 3) {
            return c.json([]);
        }
        const payload = await getPayloadHMR({
            config: configPromise,
        });
        const posts = await payload.find({
            collection: "posts",
            where: {
                or: [
                    {
                        title: {
                            like: query,
                        },
                    },
                    {
                        originalTitle: {
                            like: query,
                        },
                    },
                    {
                        description: {
                            like: query,
                        },
                    },
                ],
            },
        });
        return c.json(posts.docs);
    },
);

export default app;


If I comment out parts of code related to payload everything seems fine, otherwise I'm getting an error



Stack trace


Module not found: Can't resolve 'fs'
  2 | Object.defineProperty(exports, "__esModule", { value: true });
  3 | exports.types = exports.setConcurrency = exports.disableTypes = exports.disableFS = exports.imageSize = void 0;
> 4 | const fs = require("fs");
    | ^
  5 | const path = require("path");
  6 | const queue_1 = require("queue");
  7 | const index_1 = require("./types/index");

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/.pnpm/payload@3.0.0-beta.108_graphql@16.9.0_monaco-editor@0.38.0_react-dom@19.0.0-rc-5dcb0097-20240_qotjvbfsl2c632rzjl4krqrh4a/node_modules/payload/dist/uploads/getImageSize.js
./node_modules/.pnpm/payload@3.0.0-beta.108_graphql@16.9.0_monaco-editor@0.38.0_react-dom@19.0.0-rc-5dcb0097-20240_qotjvbfsl2c632rzjl4krqrh4a/node_modules/payload/dist/uploads/generateFileData.js
./node_modules/.pnpm/payload@3.0.0-beta.108_graphql@16.9.0_monaco-editor@0.38.0_react-dom@19.0.0-rc-5dcb0097-20240_qotjvbfsl2c632rzjl4krqrh4a/node_modules/payload/dist/collections/operations/create.js
./node_modules/.pnpm/payload@3.0.0-beta.108_graphql@16.9.0_monaco-editor@0.38.0_react-dom@19.0.0-rc-5dcb0097-20240_qotjvbfsl2c632rzjl4krqrh4a/node_modules/payload/dist/index.js
./payload.config.ts
./src/api/routes/posts.ts
./src/app/(app)/client/[[...route]]/route.ts
  • default discord avatar
    zed0547last year

    Hey

    @326267959796301824

    ,



    I'm not sure about this, but can you try

    getPayload

    instead of

    getPayloadHMR

    ?

    https://payloadcms.com/docs/beta/local-api/outside-nextjs#importing-the-payload-config-outside-of-nextjs
  • default discord avatar
    roxxellast year

    Already did, didn't help



    It's inside the next.js


    import { Hono } from "hono";
    import { handle } from "hono/vercel";
    import posts from "./routes/posts";
    
    export const runtime = "edge";
    
    const app = new Hono().basePath("/api/client");
    
    const routes = app.route("/posts", posts);
    
    export const GET = handle(app);
    export const POST = handle(app);


    With

    getPayload
    ./node_modules/.pnpm/image-size@1.1.1/node_modules/image-size/dist/index.js:4:1
    Module not found: Can't resolve 'fs'
      2 | Object.defineProperty(exports, "__esModule", { value: true });
      3 | exports.types = exports.setConcurrency = exports.disableTypes = exports.disableFS = exports.imageSize = void 0;
    > 4 | const fs = require("fs");
        | ^
      5 | const path = require("path");
      6 | const queue_1 = require("queue");
      7 | const index_1 = require("./types/index");
    
    https://nextjs.org/docs/messages/module-not-found


    For some reason it tries to import some image-size module



    Oh god I hate vercel



    removing

    export const runtime = "edge"

    resolved the issue



    But this has to be addressed, because looks like getPayload() can't be used on vercel using serverless functions

  • default discord avatar
    zed0547last year

    Yup that would do it... didn't even notice it



    Serverless or Edge? Because those two are distinct



    Pretty sure we have users who have run it Serverless

  • default discord avatar
    roxxellast year

    Edge runtime, yes



    Not familliar with vercel things, never used it

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.