Like what we’re doing? Star us on GitHub!

"Cannot find module" error on Typescript paths entries

GiS91
last week
5

Hi,



I've created a scaffold project by running

npx create-payload-app

and just added a simple

paths

entry to my Typescript configuration named

@collections

(see attached screenshot #1).



Then, just to test things out, I replaced

import Users from './collections/Users';

with

import Users from '@collections/Users';

in

payload.config.ts

(screenshot #2) and run

yarn dev

to start the server: now I get the error "Cannot find module '@collections/Users'".



What could be the problem?

Schermata_del_2023-03-13_00-07-38.png
Schermata_del_2023-03-13_00-17-08.png
Schermata_del_2023-03-13_00-18-11.png
  • jesschow
    Payload Team
    last week

    Hi @GiS91 - you just need to add

    ./

    in front of

    src/collections/*

    then you should be good to go

    Screen_Shot_2023-03-13_at_6.34.20_PM.png
  • GiS91
    last week

    Ehi @jesschow , thank you so much for your reply!



    That's what happen when you try to write code at midnight 🤣



    Anyway, the solution you propose does not seem to fix the issue. I investigated the thing a bit further and it seems that the problem relates both to

    ts-node

    and

    webpack

    .



    According to ts-node documentation [1], ts-node does not support

    paths

    resolution out of the box, so the first thing to do is to install

    tsconfig-paths

    and add it to the ts-node config as explained in the documentation.



    After that, you have to extend your webpack alias configuration in your

    payload.config.ts

    as in the attached screenshot.



    Completed the above two steps, the dev server starts normally



    [1]

    https://typestrong.org/ts-node/docs/paths/
    Schermata_del_2023-03-13_20-22-43.png
  • Jarrod
    Payload Team
    last week

    @GiS91 Yep! That worked for ya right?

  • GiS91
    last week

    @Jarrod Yesss... with those two steps it works perfectly!



    @jesschow @Jarrod Thank you for your support! ❤️

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More