0

I'm working on a Typescript Rest API with Express, that I run on a linux machine, and in one of my middleware in charge of uploading files, I create a folder in the public folder with a specific ID as a name.

It's working all fine until I run my tests with jest where I got this error :

EACCES: permission denied, mkdir './public/uploads/jobCVs/639c396892d8bf249f9c98c7'

From :

      22 |         const folderPath = `./public/uploads/jobCVs/${req.params.jobId.toString()}`;
    > 23 |         fs.mkdirSync(folderPath, { recursive: true });

This fs.mkdir right here.

I don't get why it is blocked just during my tests... and anyone know how to get the permission to allow the mkdir call even when running tests with jests ?

I tried solutions from this stackoverflow question : Error: EACCES: permission denied but still got the same error...

EDIT I'm working inside a Docker container, and launching the test command inside it resolved all access problems. I can do this like this but still don't get why it doesn't work locally (outside docker).

  • use `path.resolve(folderPath)` to check what the absolute path `folderpath` resolves to. Maybe there is some mixup with the current working directory when running via jest versus running in debug? – derpirscher Dec 16 '22 at 09:39
  • Thanks, this gave me to think that locally my project folder is in my user folder and I don't know why jest doesn't get the root access to call the mkdir. But then, since my app is in a Docker container, I tested to launch my tests inside the container, and everything went fine ! – Kévin WITZ Dec 16 '22 at 09:56
  • Have you tried in a shell session this command : `mkdir './public/uploads/jobCVs/639c396892d8bf249f9c98c7'` to see if it's successful ? – Philippe Dec 16 '22 at 10:11
  • @Philippe I got permission denied too ! Is it a problem with my global Linux User permission you think ? – Kévin WITZ Dec 16 '22 at 10:46
  • Run this and post result : `ls -ld ./public/uploads/jobCVs; id` – Philippe Dec 16 '22 at 11:52
  • @Philippe this is what I get kwitz@kevin-ThinkPad:~/code/jobsuccess/kitdocker/kitback$ ls -ld ./public/uploads/jobCVs; id drwxr-xr-x 4 root root 4096 déc. 16 10:52 ./public/uploads/jobCVs uid=1000(kwitz) gid=1000(kwitz) groups=1000(kwitz),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),134(lxd),135(sambashare),999(docker) – Kévin WITZ Dec 16 '22 at 12:16
  • You can see `jobCVs` is owned by root. You can sudo to root, and run this command `chown kwitz:kwitz ./public/uploads/jobCVs`. – Philippe Dec 16 '22 at 12:29

0 Answers0