how can I upload images on netlify? - netlify

I want to upload my images in a folder "public/uploads" but everytime I got this error:
ERROR [Error: ENOENT: no such file or directory, open '/public/uploads/352594D1-0F11-4E0F-BFDB-B28CC48A78F2.png'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/public/uploads/352594D1-0F11-4E0F-BFDB-B28CC48A78F2.png'
Upload snippet (backend)
uploadHandler = unstable_createFileUploadHandler({
maxPartSize: 10_000_000,
directory: '/public/uploads',
file: ({ filename }) => filename,
});
formData = await unstable_parseMultipartFormData(
request,
uploadHandler
);
also tried this directories :
directory: 'public/uploads',
directory: `${path.join(__dirname) + '/public/uploads}`,
but everytime I get no such directory... I deploy my site with github.
enter image description here

Related

Error: ENOENT: no such file or directory, unlink even tho the file exists

As you can see, the directory exists and the file too, but for some reason, it's not being able to find the file to delete it.
Directory exists
Code:
const fs = require('fs')
const path = "./resources/castigos/silencios/705143456384811039/526122991042428959.json"
fs.unlink(path, (err) => {
if (err) {
console.error(err)
return
}
//file removed
})
I tried running the project as root, nothing...
[Error: ENOENT: no such file or directory, unlink './resources/castigos/silencios/705143456384811039/526122991042428959.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'unlink',
path: './resources/castigos/silencios/705143456384811039/526122991042428959.json'
}
In your image file name is different, I believe wrong screen shared,
Also whenever dealing with fs and file paths use path module and __dirname
you can update your path as below
const path = require('path');
const filePath = path.join(__dirname, '/resources/castigos/silencios/705143456384811039/526122991042428959.json');

nodeJS path module can't resolve the directory path

I want to create a CLI for bootstrap my project. Everything works find but in order to copy my template files I use ncp where I give the template directory as a argument. So I tried,
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
const access = promisify(fs.access);
...
const templateDir = path.resolve(
new URL(import.meta.url).pathname,
'../../templates',
'project_name'
);
try {
await access(templateDir, fs.constants.R_OK);
} catch (err) {
console.error('%s Invalid template name', chalk.red.bold('ERROR'));
process.exit(1);
}
...
I got ERROR Invalid template name
Then I tried to figure out the problem,
//* after getting ERROR Invalid template name
//* checking file exist or not
fs.stat(templateDir, function (err, stat) {
if (err == null) {
console.log('File exists');
} else if (err.code === 'ENOENT') {
// file does not exist
console.log("file doesn't exit");
} else {
console.log('Some other error: ', err.code);
}
});
file doesn't exit
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
ERROR Invalid template name
I expect the access should be G:\CLI%20project\create-project\templates\javascript but it's not giving the that. where was the wrong happened? by the way I use esm module loader.
Give your errors a good read.
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
They're not trying to trick you. They're just telling you/
G:\\G:\\
Error messages are there for a reason .
I had the same issue and found the answer:
const templateDir = path.resolve(
new URL(import.meta.url).pathname, // This is where it gives the error
'../../templates',
options.template
);
Change new URL (import.meta.url).pathname to __filename. It worked for me.

fs.writeFile not saving even when path is correct

Having an issue with fs.writeFile in my nodejs app running locally where im getting an error of this below,
I am running on localhost xampp also on windows if that may be an issue?
[Error: ENOENT: no such file or directory, open 'C:\Users\exampleuser\Desktop\examplenodejspath\product\sku123.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\exampleuser\\Desktop\\examplenodejspath\\product\\sku123.json'
}
Below is a copy of the code.
var product = {"SKU": "sku123","name": "test"};
fs.writeFile(__dirname + "/product/" + product.SKU + ".json", product, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("product was saved!");
});
I can confirm the path is correct. but for some reason it still returns its not correct.
Any help would be appericated

Uploading files error with express-fileUpload

I have an error uploading a file on the server if I can help
This is a command to get the file from the client and save it to the uploads folder
exports.adminSendFile = (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: 'No file uploaded' });
}
const file = req.files.file;
file.mv(`${__dirname}/SERVER/Servises/uploads/${file.name}`, err => {
console.log("****HERE*****");
if (err) {
console.error(err);
return res.status(500).send(err)
}
res.json({
fileName: file.name,
filePath: `/SERVER/Servises/uploads/${file.name}`
})
console.log(req.body),
vacations.create({
description: req.body.description,
destination: req.body.destination,
createdAt: req.body.createdAt,
updatedAt: req.body.updatedAt,
img: req.body.filePath,
price: req.body.price,
})
})
Imge>>>Clike-Me
You can see the Error here
{ [Error: ENOENT: no such file or directory, open 'C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\controller\SERVER\Servises\uploads\My Passport Photo.jpg']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\\Users\\Itzik\\Desktop\\לימודים\\projact3\\server\\Servises\\controller\\SERVER\\Servises\\uploads\\My Passport Photo.jpg' }
POST /users/admin/upload 500 40.394 ms - 189
__dirname is the most common method. It gives you the path of the currently running file.
As per your error , I think what path you are trying to get is this :
C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\uploads\My Passport Photo.jpg'
And not :
C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\controller\SERVER\Servises\uploads\My Passport Photo.jpg'
You need to go one step up from current directory with ../ and then you are good to go, like this :
file.mv(`${__dirname}/../uploads/${file.name}`

FFMpeg incorrect execution calling two different paths in NodeJS

I'm trying to take screenshots from a movie file and my app crashes with the following error:
$ FFMPEG_PATH=C:\\Apps\\ffmpeg\\bin\\ node .
=====Convert Video Failed======
{ [Error: spawn c:\Apps\ffmpeg\bin\ffprobe.exe
c:\Program Files (x86)\ImageMagick-6.8.3-Q16\ffprobe.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
path: 'c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
spawnargs:
[ '-show_streams',
'-show_format',
'j:\\some.avi' ] }
stdout: undefined
stderr: undefined
As you can see I'm passing a the FFMPEG_PATH env variable because otherwise I'm getting a similar error:
$ node .
=====Convert Video Failed======
{ [Error: spawn c:\Apps\ffmpeg\bin\ffprobe.exe
c:\Program Files (x86)\ImageMagick-6.8.3-Q16\ffprobe.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
path: 'c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
spawnargs:
[ '-show_streams',
'-show_format',
'j:\\some.avi' ] }
stdout: undefined
stderr: undefined
In both cases you can see that the command that node/fluent-ffmpeg is using results in a double path like this: c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe which obviously fails.
What causes this and how do I fix it?
Win 7, Node v4.1.2, ffmpeg version N-76041-g0418541
The code I'm using:
var ffmpeg = require('fluent-ffmpeg');
// ffmpeg.setFfprobePath("c:\\Apps\\ffmpeg\\bin\\ffprobe.exe");
var filename = 'j:\\some.avi';
var command = ffmpeg(filename);
// Code from an example
command
.on('filenames', function(filenames) {
console.log('Will generate ' + filenames.join(', '))
})
.on('end', function() {
console.log('Screenshots taken');
})
.on('error', function(err, stdout, stderr) {
console.log(" =====Convert Video Failed======");
console.log(err);
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
})
.screenshots({
// Will take screens at 20%, 40%, 60% and 80% of the video
count: 4,
folder: 'd:\\projects\\pics'
})
It only happens in Git Bash - which I got used to working in window with. Running the script in normal cmd works properly.

Resources