I have a problem in this part of my code,with COMMONJS and it's working, my question is how can I make it work on ESM, I've tried this but not workiing :
import express from 'express';
const router = express.Router();
import cloudinary from 'cloudinary';
import { unlinkSync } from 'node:fs';
const router = require('express').Router();
const cloudinary = require('cloudinary');
const { unlinkSync } = require('node:fs');
// we will upload image on cloudinary
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
// Upload image only admin can use
router.post('/upload', (req, res) => {
try {
cloudinary.v2.uploader.upload(file.tempFilePath, { folder: 'test' }, async (err, result) => {
if (err) throw err;
removeTmp(file.tempFilePath);
res.json({ public_id: result.public_id, url: result.secure_url });
});
} catch (err) {
return res.status(500).json({ msg: err.message });
}
});
const removeTmp = (path) => {
unlinkSync(path, err => {
if(err) console.log('this is the error',err);;
});
console.log(`successfully deleted ${path}`);
};
module.exports = router;
so basically, I separated my route with my controller an put the cloudinary config inside the scope of the function: