payload icon indicating copy to clipboard operation
payload copied to clipboard

`generate:types` Fails with Endpoint in Collection Configuration

Open peterkunsay opened this issue 2 years ago • 3 comments

Link to reproduction

No response

Payload Version

3.0.0-beta.43

Node Version

v20.9.0

Next.js Version

15.0.0-rc.0

Describe the Bug

Hello,

Firstly I just want to say that I absolutely love payload, I have been using it since v1 and it is hands down the best CMS in the scene.

I have a collection called endUsers:

const EndUsers: CollectionConfig = {
    slug: 'endUsers',
    auth: {
        verify: {
            generateEmailSubject: () => 'Mail subject test',
            generateEmailHTML: ({ token }) => 'Mail html test ${token}'
        },
        forgotPassword: {
            generateEmailSubject: () => 'Mail subject test',
            generateEmailHTML: (token) => 'Mail html test ${token}'
        },
        tokenExpiration: 14400,
    },
    admin: {
        useAsTitle: 'email',
        group: {
            en: getStaticTranslation('en', 'Admin.Groups.EndUser'),
            hu: getStaticTranslation('hu', 'Admin.Groups.EndUser'),
        }
    }...

My getStaticTranslation function:

const getStaticTranslation = (langCode: string | undefined, keyPath: string): string => {
   if (!langCode) langCode = defaultLanguage;
   try {
       const translations = require(`../../../locales/${langCode}.json`);
       const nestedTranslation = getNestedTranslation(translations, keyPath);
       if (typeof nestedTranslation === 'string') {
           return nestedTranslation;
       } else {
           return keyPath;
       }
   } catch (error) {
       console.error(`Error loading translations for language code ${langCode}:`, error);
       return keyPath;
   }
};

The reason I use this function is because I want to have one single translation json for both the application and for payload.

If I use generate:types without any endpoint in the collection, it generates the types correctly with some errors: Error loading translations for language code en: ReferenceError: require is not defined. Now this would not be an issue, translations do not matter during type generation, and since the types are correctly generated this is not a problem for me.

The issue occurs when I use any form of endpoint inside this collection. When I have an endpoint like:

endpoints: [
        {
            path: '/register',
            method: 'post',
            handler: async (req) => {

                return NextResponse.json({ error: "hi" }, { status: 500 });

            }
        },
    ],

I get the following error during generate:types :

pnpm run generate:types

> [email protected] generate:types D:\Projects\new-semi-product
> payload generate:types

Error resolving path: [Error: ENOENT: no such file or directory, lstat 'D:\Projects\new-semi-product\node_modules\D:\Projects\new-semi-product\node_modules\.pnpm\[email protected][email protected]_react-dom@19_u5g2i42qnvpr6u6rbf5n5egehi\node_modules\next\server.js'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'lstat',
  path: 'D:\\Projects\\new-semi-product\\node_modules\\D:\\Projects\\new-semi-product\\node_modules\\.pnpm\\[email protected][email protected]_react-dom@19_u5g2i42qnvpr6u6rbf5n5egehi\\node_modules\\next\\server.js'      
}

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:406:5)
    at validateString (node:internal/validators:162:11)
    at pathToFileURL (node:url:1024:3)
    at resolve (file:///D:/Projects/new-semi-product/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]__@[email protected][email protected][email protected]/node_modules/payload/dist/bin/loader/index.js:79:18)
    at async nextResolve (node:internal/modules/esm/hooks:833:22)
    at async Hooks.resolve (node:internal/modules/esm/hooks:278:24)
    at async handleMessage (node:internal/modules/esm/worker:168:18) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v20.9.0
 ELIFECYCLE  Command failed with exit code 1.

This error is persistent even if I do not use my translation fn anywhere inside the collection.

My package.json:

{
  "name": "next-latest-starter",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "cross-env NODE_OPTIONS=--no-deprecation next dev",
    "devturbo": "cross-env NODE_OPTIONS=--no-deprecation next dev --turbo",
    "devsafe": "rd /s /q .next && cross-env NODE_OPTIONS=--no-deprecation next dev",
    "build": "cross-env NODE_OPTIONS=--no-deprecation next build",
    "payload": "cross-env NODE_OPTIONS=--no-deprecation payload",
    "start": "cross-env NODE_OPTIONS=--no-deprecation next start",
    "lint": "cross-env NODE_OPTIONS=--no-deprecation next lint",
    "ci": "payload migrate && pnpm build",
    "generate:types": "payload generate:types",
    "standalone-script": "node ./src/scripts/standalone-script.js"
  },
  "engines": {
    "node": "^18.20.2 || >=20.9.0"
  },
  "dependencies": {
    "@payloadcms/db-mongodb": "3.0.0-beta.43",
    "@payloadcms/db-postgres": "3.0.0-beta.43",
    "@payloadcms/next": "3.0.0-beta.43",
    "@payloadcms/richtext-lexical": "3.0.0-beta.43",
    "@payloadcms/richtext-slate": "3.0.0-beta.43",
    "@payloadcms/ui": "3.0.0-beta.43",
    "babel-plugin-react-compiler": "^0.0.0-experimental-592953e-20240517",
    "cross-env": "^7.0.3",
    "graphql": "^16.8.1",
    "idb": "^8.0.0",
    "next": "15.0.0-rc.0",
    "next-intl": "^3.14.1",
    "payload": "3.0.0-beta.43",
    "react": "19.0.0-rc-f994737d14-20240522",
    "react-country-flag": "^3.1.0",
    "react-dom": "19.0.0-rc-f994737d14-20240522",
    "react-icons": "^5.2.1",
    "sharp": "0.32.6"
  },
  "devDependencies": {
    "@types/node": "^20.12.12",
    "@types/react": "npm:[email protected]",
    "@types/react-dom": "npm:[email protected]",
    "eslint": "^8.57.0",
    "eslint-config-next": "14.2.3",
    "typescript": "^5.4.5"
  },
  "pnpm": {
    "overrides": {
      "@types/react": "npm:[email protected]",
      "@types/react-dom": "npm:[email protected]"
    }
  },
  "overrides": {
    "@types/react": "npm:[email protected]",
    "@types/react-dom": "npm:[email protected]"
  }
}

Maybe I am doing something incorrectly; I apologize if this is not considered an issue.

Thank you for your hard work!

Reproduction Steps

  • Cloned the demo repo at beta.39
  • Continously updated it with the latest payload
  • Made collection with endpoint
  • Ran generate:types

Adapters and Plugins

db-mongodb

peterkunsay avatar Jun 11 '24 08:06 peterkunsay

@AlessioGr this has to do with the node v20 and something in our bin/loader file. I think the endpoints is just a red herring.

JarrodMFlesch avatar Jun 14 '24 12:06 JarrodMFlesch

Hey @peterkunsay sadly I was not able to reproduce this issue. I have published my reproduction attempt here: https://github.com/payloadcms/payload-3.0-demo/tree/repro/6718 copied your exact collection config and package.json and it works just fine for me, even if the endpoint is enabled. I'm also on the same node version as you (20.9.0):

CleanShot 2024-06-14 at 21 10 34

Could you open up a fresh, new issue with a linked minimal reproduction repository? Here is our reproduction guide: https://github.com/payloadcms/payload/blob/main/.github/reproduction-guide.md

It might be something very specific to your project. Tsconfig, monorepo, could really be anything which is why we need a minimal reproduction to be able to investigate it. Gonna close this to keep our issues clean, but I would really appreciate if you could open up a new issue reproducting this - will then look into this ASAP!

AlessioGr avatar Jun 15 '24 01:06 AlessioGr

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Sep 07 '24 02:09 github-actions[bot]