'Command line save file error in Electron.js

I have followed a course on Pluralsight named Electron Fundamentals: https://app.pluralsight.com/library/courses/electron-fundamentals/table-of-contents.

I am up to the final project in the video showing image saving. I finished the video, only to find out that my code does not work. I am a beginner in Electron and Javascript. The problem is that my code runs successfully with a error in the command line, takes photos, but it does not save them. I have shown the command line error and the code from photo.js and main.js. I do not think the problem is from the code, since when I ran the source code, It showed the same error. I might be wrong on that assumption, but I think it's a system error. I'm not sure what is wrong or how to fix it.

I believe it is fixed when I use Date.now(), but I would like to be able to use Date().

Command line error:

{ Error: ENOENT: no such file or directory, open 'C:\Users\user\Pictures\photobombth\Tue Dec 15 2020 10:27:00 GMT+1300 (New Zealand Summer Time).png'
    at Error (native)
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\user\\Pictures\\photobombth\\Tue Dec 15 2020 10:27:00 GMT+1300 (New Zealand Summer Time).png' }

Here is my code.

Photo.js:

const fs = require('fs')
const path = require('path')

const logError = err => err && console.error(err)

exports.save = (picturesPath, contents) => {
    const base64Data = contents.replace(/^data:image\/png;base64,/, '')
    const imgPath = path.join(picturesPath, `${new Date()}.png`)
    fs.writeFile(imgPath, base64Data, { encoding: 'base64' }, logError)
}

exports.getPicturesDir = app => {
    return path.join(app.getPath('pictures'), 'photobombth')
}
exports.mkdir = picturesPath => {
    fs.stat(picturesPath, (err, stats) => {
        if (err && err.code !== 'ENOENT')
            return logError(err)
        else if (err || !stats.isDirectory())
            fs.mkdir(picturesPath, logError)
    })
}

Main.js:

const electron = require('electron')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

const images = require('./images')

const { app, BrowserWindow, ipcMain: ipc } = electron

let mainWindow = null

app.on('ready', _ => {
    mainWindow = new BrowserWindow({
        width: 1200,
        height: 725,
        resizable: false
    })

    mainWindow.loadURL(`file://${__dirname}/capture.html`)

    mainWindow.webContents.openDevTools()

    images.mkdir(images.getPicturesDir(app))

    mainWindow.on('closed', _ => {
        mainWindow.null
    })
})

ipc.on('image-captured', (evt, contents) => {
    images.save(images.getPicturesDir(app), contents)
})

Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source