I am quite new to Node and hence stuck with this!!
I am trying to write a node script that makes a folder (lizardproject) which has three different files i.e. index.html, app.js, style.css.
Code
const fs = require('fs');
const folderName = process.argv[2] || 'Project'
try {
fs.mkdirSync(folderName);
fs.writeFileSync(`${folderName}/index.html`)
fs.writeFileSync(`${folderName}/app.js`)
fs.writeFileSync(`${folderName}/style.css`)
} catch(e) {
console.log("Something went wrong!!");
console.log(e);
}
On execution, the folder is being created.
Execution
hp\nodeintro>node boilerplate.js lizardproject
Result
hp\nodeintro>ls
boilerplate.js lizardproject
But the files are not, and this error is being displayed:-
Error
Something went wrong!!
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
at Object.writeFileSync
Look at the documentation for that function:
fs.writeFileSync(file, data[, options])
file <string> | <Buffer> | <URL> | <integer> filename or file descriptor data <string> | <Buffer> | <TypedArray> | <DataView> | <Object>
data is the second argument.
You aren't providing the second argument so it is undefined. Hence the error message: Received undefined at Object.writeFileSync.
The error tells you what types of values are acceptable. Provide one of those. A string would be simplest.