0

while uploading file and creating a path , I am getting creating a folder error :-

Error: EACCES: permission denied, mkdir '/opt/bitnami/apps/NodeJS-Login/uploads'
    at Object.fs.mkdirSync (fs.js:885:18)
    at Function.sync (/opt/bitnami/apps/NodeJS-Login/node_modules/mkdirp/index.js:71:13)
    at new DiskStorage (/opt/bitnami/apps/NodeJS-Login/node_modules/multer/storage/disk.js:21:12)
    at module.exports (/opt/bitnami/apps/NodeJS-Login/node_modules/multer/storage/disk.js:65:10)
    at new Multer (/opt/bitnami/apps/NodeJS-Login/node_modules/multer/index.js:15:20)

I am using bitnami on AWS to host my MEAN app.

on my main server.js file I have added this:-

app.use(multer({ dest: './uploads/',
 rename: function (fieldname, filename) {
   return filename;
 },
}));

on schema model :-

companyLogo: {
                data: Buffer,
                type: String
                }

and in controller for route :-

admin.companyLogo = fs.readFileSync(req.files.comLogo.path)
admin.companyLogo.type = 'image/png';

What should I do to make image upload ? Also do I have to pass other key values in form-data instead of raw ?

NoobCoder
  • 493
  • 8
  • 25
  • 1
    You should probably fix the EACCES permission error. – TGrif Jun 11 '19 at 10:33
  • @TGrif What is this error is about ? I am looking but all the ques is about local project. I am not getting why it is not creating folder on the hosted server – NoobCoder Jun 11 '19 at 10:35

1 Answers1

0

/opt is write protected by default, so here are possible fixes

1) Change permissions for /opt and allow the user to write in this folder (Not Recommended)

OR

2) Run the server.js with the super user, this way you have complete right over the directory and it will allow you to do anything (Not Recommended)

OR

3) Just change the path to somewhere the user has access to write (Recommended)

Mehul Mittal
  • 134
  • 10
  • This is already been hosted. So where do I suppose to give user to write access – NoobCoder Jun 11 '19 at 10:39
  • use chmod -R 777 folderpath on bitnami console. But As I mentioned it is not recommended. It would be simple and better if you just go with option 3 – Mehul Mittal Jun 11 '19 at 10:45
  • If the project is hosted in bitnami then the folder to access uploading files should be there only I guess. – NoobCoder Jun 11 '19 at 10:47
  • True for superuser, not for every user, other users can read only, no write hence no upload – Mehul Mittal Jun 11 '19 at 10:54
  • But other user is secondary thing. The error is coming when I am trying to run the server only. Who is superuser here exactly ? – NoobCoder Jun 11 '19 at 11:18
  • superuser is commonly referred to as "root" or if you are more comfortable with windows then it is the administrator; when you write sudo you mean to run the following command as superuser and not the logged in user – Mehul Mittal Jun 11 '19 at 14:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194770/discussion-between-mehul-mittal-and-noobcoder). – Mehul Mittal Jun 11 '19 at 14:27