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.

Unable to write files using .fs nodejs

default discord avatar
eddiedev2 years ago
4

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
    2 years 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
    eddiedev2 years ago

    lol ty! how would i adjust my stub file?

  • default discord avatar
    uljanovs11 months ago

    any idea how to import pdfkit, I am stuck on last bit as you discussed but readFileSync / readFile is used within pdfkit so can't control it



    ERROR in ./node_modules/fontkit/dist/module.mjs 38:17-36
    export 'readFileSync' (imported as '$5OpyM$readFileSync') was not found in 'fs' (possible exports: default)
    
    ERROR in ./node_modules/fontkit/dist/module.mjs 46:4-19
    export 'readFile' (imported as '$5OpyM$readFile') was not found in 'fs' (possible exports: default)
  • default discord avatar
    notchr11 months ago

    I see some mixed answers on this, and I'm not super familiar with fontkid / pdfkit



    https://github.com/foliojs/fontkit/issues/77

    A lot of the upvoted answers say that the font needs to be loaded in as a buffer and that fs is not an option



    I'm not sure exactly how this works in relation to Payload, but I would try checking out some of the relevant SO posts or on their Github issues

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.