0

I am trying to write a basic JS script in NodeJs. The script will create a folder with the name of the folder to be reponses_timestamp.

I have written the attached script, however, when it runs, i receive an error which says:

Einval: invalid argument.

Any ideas on how to fix this?

Test.js

const fs = require('fs');
const today = new Date();
const date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
const time = today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
const dateTime = date + '_' + time;
const uniqueIdentifier = dateTime;
// const folderName = './responses' + '_' + uniqueIdentifier;

try {
    if (!fs.existsSync('./responses' + '_' + uniqueIdentifier)) {
    fs.mkdirSync('./responses' + '_' + uniqueIdentifier)
  }
} catch (err) {
  console.error(err)
}

enter image description here

1
  • did either answer help you with your issue? Commented Jun 10, 2021 at 20:10

2 Answers 2

1

Probably - the problem is with the folder name. In Windows folder should not have special characters, like :

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned the issue is due to the usage of the colon : because Windows (also per your screenshot Windows shown as the path) will not allow special characters.

It wasn't mentioned a solution so I wanted to mention for what you're doing this can be achieved easily with moment js using two lines of code and a substitution of the colon for a dash:

const date = new Date()
const uniqueIdentifier = moment(date).format('YYYY-MM-DD-HH-MM')

console.log('uuid', uniqueIdentifier)

// Result: "uuid" "2021-05-13-11-05"

and even a one-liner:

const uniqueIdentifier = moment(new Date()).format('YYYY-MM-DD-HH-MM')

console.log('uuid', uniqueIdentifier)

// Result: "uuid" "2021-05-13-11-05"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.