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.

payload generate:types ERR_MODULE_NOT_FOUND

default discord avatar
itslamelast year
15

Hi, I have the following error when I try to run the

payload generate:types

command.


➜ payload generate:types
node:internal/modules/package_json_reader:268
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@/contents' imported from /Users/itslame/Documents/repos/itslame/src/payload.config.ts
    at Object.getPackageJSONURL (node:internal/modules/package_json_reader:268:9)
    at packageResolve (node:internal/modules/esm/resolve:768:81)
    at moduleResolve (node:internal/modules/esm/resolve:854:18)
    at defaultResolve (node:internal/modules/esm/resolve:984:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:685:12)
    at #cachedDefaultResolve (node:internal/modules/esm/loader:634:25)
    at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:384:53)
    at new ModuleJobSync (node:internal/modules/esm/module_job:341:34)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:357:11)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1385:24) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v22.14.0

I decided to run this command because I noticed that my

payload-types.ts

file is not auto-generated when I added new fields in my collection. Not sure what happened because it worked fine so far and I've not added/updated any packages.

  • default discord avatar
    traderlaunchpadlast year

    you may need to add



     "paths": {
          "@/*": ["./src/*"],

    to your tsconfig



    your issue is with @/components path resolution

  • default discord avatar
    itslamelast year

    yup I already have this path alias setup and it's working throughout the project

  • default discord avatar
    traderlaunchpadlast year

    share your repo, or folder structure and tsconfig content.



    i have a lot of these issues in a large monorepo so should be able to help

  • default discord avatar
    itslamelast year

    my folder structure current looks like this



    tsconfig.json


    {
      "compilerOptions": {
        "target": "ES2017",
        "lib": [
          "dom",
          "dom.iterable",
          "esnext"
        ],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "plugins": [
          {
            "name": "next"
          }
        ],
        "paths": {
          "@/*": [
            "./src/*"
          ],
          "@payload-config": [
            "./src/payload.config.ts"
          ]
        }
      },
      "include": [
        "next-env.d.ts",
        "**/*.ts",
        "**/*.tsx",
        ".next/types/**/*.ts"
      ],
      "exclude": [
        "node_modules"
      ]
    }


    for further context i tried importing via "./" (without alias) and was still faced with a similar error:


    payload generate:types
    node:internal/modules/esm/resolve:275
        throw new ERR_MODULE_NOT_FOUND(
              ^
    
    Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/itslame/Documents/repos/itslame/src/contents/collections/Users' imported from /Users/itslame/Documents/repos/itslame/src/payload.config.ts
  • default discord avatar
    traderlaunchpadlast year

    in your



    {
      "compilerOptions": {
        /* Strictness */
        "strict": true,
        "noUncheckedIndexedAccess": true,
        "noImplicitOverride": true,
    
        "baseUrl": ".",
        "esModuleInterop": true,
        "target": "ES2022",
        "lib": ["DOM", "DOM.Iterable", "ES2022"],
        "allowJs": true,
        "skipLibCheck": true,
        "strictNullChecks": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "incremental": true,
        "jsx": "preserve",
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "sourceMap": true,
        "isolatedModules": true,
        "plugins": [
          {
            "name": "next"
          }
        ],
        "paths": {
          "@payload-config": ["./src/payload.config.ts"],
          "react": ["./node_modules/@types/react"],
          "@/*": ["./src/*"]
        }
      },
      "include": [
        "**/*.ts",
        "**/*.tsx",
        ".next/types/**/*.ts",
        "redirects.js",
        "next.config.js",
        "next-sitemap.config.cjs",
        "payload.config.ts"
      ],
      "exclude": ["node_modules"]
    }


    here is mine from my base create-payload-app website tempalte



    try and copy exactly, see if you have the same issue, could be one of the tsconfig props here like baseUrl



    i think you need to set baseurl and paths, paths use the baseurl as the reference so @/components = ../src/components, but atm it doesnt know to link @ to ./

  • default discord avatar
    itslamelast year

    just tried copy pasting ur config and still the same error :c



    i just dont quite get why this throws out an error too even when given absolute path

  • default discord avatar
    traderlaunchpadlast year

    paste your package.json



    do you have "type": "module", set

  • default discord avatar
    itslamelast year

    this seemed to do the trick!


    payload generate:types
    [23:39:53] INFO: Compiling TS types for Collections and Globals...
    [23:39:53] INFO: Types written to /Users/itslame/Documents/repos/itslame/src/payload-types.ts
  • default discord avatar
    traderlaunchpadlast year

    nice!

  • default discord avatar
    itslamelast year

    btw what's the "type" field for

  • default discord avatar
    traderlaunchpadlast year
    https://stackoverflow.com/questions/61401475/why-is-type-module-in-package-json-file

    When you have "type": "module" in the package.json file, your source code should use import syntax. When you do not have, you should use require syntax; that is, adding "type": "module" to the package.json enables ES 6 modules. For more info, see here.

  • default discord avatar
    itslamelast year

    oh i see got it thanks!

  • default discord avatar
    traderlaunchpadlast year

    use

    /solve

    to solve this thread if complete



    no problem!

  • default discord avatar
    itslamelast year

    oh didn't know that

  • default discord avatar
    traderlaunchpadlast year

    🙂

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.