Can we create a new file with code?

I am wanting to know how can I make a new file by javascript in my project. And how to type something in that.

1 Like

Hey @4-AK,

You can use the fs module to create and edit files using JavaScript. Here’s how:

const fs = require('fs');

fs.writeFile('script.js', 'FILE_CONTENT', function (err) {
  if (err) throw err;
  console.log('Done!');
});
 

Note that fs.writeFile rewrites the whole content in a file. Use fs.appendFile to add contents to existing file contents.

Hope this helps!

1 Like

thank you very much sir.

You’re welcome, but please don’t call me ‘sir’. :grin::joy: Just a regular thirteen year old boy!

1 Like

lol i can see the awkwardness in here

Hey khalby786 My Javascript Shut Down In 2019.

Tom Cruise Confused

4 Likes

Then why Javascript still used in 2021?