Unable to write files using .fs nodejs

default discord avatar
eddieDev
5 months ago
10

Hello, currently I'm trying to import fs (node module) and use it to write a file for Media collections s3 authentication.


However, when I connect to the beforeChange hook, even though the file is server side, I am getting a module not found error: Can't resolve 'fs'.


Any ideas? Thanks.



I've tried following this documentation:

https://payloadcms.com/docs/admin/webpack#aliasing-server-only-modules

, but I'm still getting the same error.



I got the errors to go away, but now I am facing this Watchpack error:


Watchpack Error (initial scan): Error: ENOTDIR: not a directory, scandir 'C:\Users\eddieDev\Developer\cms\src\api\s3\actions\fetchS3Client.ts'



The directory is correct. I tried deleting the node_modules cache as well.



Now the watchpack error is gone, but now I'm getting this warning:


[22:49:40] INFO (payload): WARNING in ./src/api/s3/actions/fetchS3Client.ts 21:34-46


export 'readFileSync' (imported as 'readFileSync') was not found in 'fs' (possible exports: default)



Should I be getting this warning at all? Or am I missing something?



This may help someone else in the future. I just figured it out. I had to use require('fs') and the warnings are gone now. So that's good to know!

  • discord user avatar
    jmikrut
    Payload Team
    4 months ago

    I liked following along with your train of thought here, haha



    yes, basically you solved issue by issue until you properly fixed and implemented an alias



    but then, it's worth noting that you don't

    need

    to use

    require

    . Instead, the last error was reported because your "stub file" for FS did not export a const of

    readFileSync

    - and it only exported a

    default

    . So if you just adjusted your stub file to export the functions you were attempting to import, it would have worked that way too

  • default discord avatar
    eddieDev
    4 months ago

    lol ty! how would i adjust my stub file?

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.