Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 - Using NextResponse causes a module-not-found error #5320

Closed
davelsan opened this issue Mar 13, 2024 · 2 comments
Closed

V3 - Using NextResponse causes a module-not-found error #5320

davelsan opened this issue Mar 13, 2024 · 2 comments
Labels
status: needs-triage Possible bug which hasn't been reproduced yet v3

Comments

@davelsan
Copy link

Link to reproduction

No response

Describe the Bug

When using custom endpoints, the official docs say to use NextResponse to send the response object, which is imported from next/server.

However, doing this prevents the payload generate:types command from running due to next/server not being found.

> cross-env NODE_OPTIONS=--no-deprecation payload "generate:types"

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/<path/to/project>/node_modules/next/server' imported from /<path/to/project/endpoint-function.ts>`
Did you mean to import next@14.2.0-canary.10_react-dom@18.2.0_react@18.2.0/node_modules/next/server.js?
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:917:10)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at nextResolve (node:internal/modules/esm/hooks:865:28)
    at resolve (file:///<path/to/project>/node_modules/.pnpm/payload@3.0.0-alpha.48_@swc+core@1.4.7_@swc+types@0.1.5/node_modules/payload/dist/bin/register/index.js:59:12)
    at nextResolve (node:internal/modules/esm/hooks:865:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:303:30)
    at handleMessage (node:internal/modules/esm/worker:196:24)
    at Immediate.checkForMessages [as _onImmediate] (node:internal/modules/esm/worker:138:28)
    at process.processImmediate (node:internal/timers:478:21) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///<path/to/project>/node_modules/next/server'
}

To Reproduce

Create a custom endpoint in a collection and use NextResponse in the handler.

// some collection
  endpoints: [
    {
      path: '/route/:param',
      method: 'get',
      handler,
    },
  ],
import { NextResponse } from 'next/server';

export const handler: PayloadHandler = async (req) => {
  try {
    return NextResponse.json(someResponse, { status: OK });
  } catch (error) { }
}

Payload Version

3.0.0-alpha.48

Adapters and Plugins

"@payloadcms/db-mongodb": "3.0.0-alpha.48", "@payloadcms/next": "3.0.0-alpha.48", "@payloadcms/plugin-cloud-storage": "3.0.0-alpha.48", "@payloadcms/plugin-nested-docs": "3.0.0-alpha.48", "@payloadcms/richtext-lexical": "3.0.0-alpha.48",

@davelsan davelsan added the status: needs-triage Possible bug which hasn't been reproduced yet label Mar 13, 2024
@davelsan
Copy link
Author

A follow-up on this issue.

Using Response instead of NextResponse in the handler function does not cause this error. Also, using NextResponse within a custom route file, like the one in the alpha demo, does not cause this error either.

As a side note, destructuring the http-status was causing a syntax error when generating types only, but I checked the source and the aliases are exported. Not sure if it could be related.

// SyntaxError: The requested module 'http-status' does not provide an export named 'OK'
import { OK } from 'http-status';

// But this is fine
import HttpStatus from 'http-status';
Response.json(someResponse, { status: HttpStatus.OK });

@PatrikKozak PatrikKozak added the v3 label Apr 3, 2024
@davelsan
Copy link
Author

Fixed in beta.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet v3
Projects
None yet
Development

No branches or pull requests

2 participants