[ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received null

Issue

I’m having this problem while trying to upload phtos and I don’t know why. I’m using multer v. 2.0.0 and express. Is my multer version too old ? Or is it a problem with the await pipeline()?

In upload.js :

const UserModel = require("../models/user.model");
const fs = require("fs");
const { promisify } = require("util");
const pipeline = promisify(require("stream").pipeline);
const { uploadErrors } = require("../utils/errors.utils");

module.exports.uploadProfile = async (req, res) => {
  try {
    if (
      req.file.detectedMimeType != "image/jpg" &&
      req.file.detectedMimeType != "image/png" &&
      req.file.detectedMimeType != "image/jpeg"
    )
      throw Error("invalid file");

    if (req.file.size > 500000) throw Error("max size");
  } catch (err) {
    const errors = uploadErrors(err);
    return res.status(201).json({ errors });
  }
  const fileName = req.body.name + ".jpg";

  await pipeline(
    req.file.stream,
    fs.createWriteStream(
      `${__dirname}/../client/public/uploads/profil/${fileName}`
    )
  );
};

In user.routes.js :

const router = require("express").Router();
const multer = require("multer");
const upload = multer();
const userController = require("../controllers/user.controller");
const authController = require("../controllers/auth.controller");
const uploadController = require("../controllers/upload.controller");

// auth
router.post("/register", authController.signUp);
router.post("/login", authController.signIn);
router.get("/logout", authController.logout);
//user display block
router.get("/", userController.getAllUsers);
router.get("/:id", userController.userInfo);
router.put("/:id", userController.updateUser);
router.delete("/:id", userController.deleteUser);
router.patch("/follow/:id", userController.follow);
router.patch("/unfollow/:id", userController.unfollow);
// upload
router.post("/upload", upload.single("file"), uploadController.uploadProfile);
module.exports = router;

Can anyone help me ?

Solution

I think we are on the same problem.
You have to go back to the previous version of multer not on 2.0.0 and add sharp js to replace pipeline

npm i [email protected] sharp

upload.controller.js

const { uploadErrors } = require("../utils/errors.utils.js");
const sharp = require("sharp");

//upload image profil utilisateur
module.exports.uploadProfil = async (req, res) => {
  //console.log(req.file);
  //renome le fichier avec extension .jpg
  const fileName = req.body.name +".jpg"; 
  try {
    if (
      req.file.mimetype != "image/jpg" &&
      req.file.mimetype != "image/png" &&
      req.file.mimetype != "image/jpeg"
    )
      throw Error("invalid file");

    if (req.file.size > 500000) throw Error("max size");
  } catch (err) {
    const errors = uploadErrors(err);
    return res.status(201).json({ errors });
  }

  try {
    await sharp(req.file.buffer)
      .resize({ width: 150, height: 150 }) 
      .toFile(`${__dirname}/../client/public/uploads/profil/${fileName}`
      );
    res.status(201).send("Photo de profil chargé avec succés");
  } catch (err) {
    res.status(400).send(err);
  }
}


    

Answered By – Grafiti 54

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published