Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there equivalent for file_put_contents in javascript?

can I ask , what is the equivalent for file_put_contents in javascript ?

Thank you in advance.

like image 720
jemz Avatar asked May 04 '15 17:05

jemz


2 Answers

In JavaScript? No.

In NodeJS? Yes, it's called writeFile. Here's the example in the documentation:

fs.writeFile('message.txt', 'Hello Node', function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
});
like image 193
T.J. Crowder Avatar answered Nov 17 '22 05:11

T.J. Crowder


Just to add to the reply above if you don't want to overwrite the content of the file but rather append to it

var fs = require('fs'); // don't forget to require in the header or it won't work

fs.writeFile(
    "foo.txt", // file
    "bar", // string
    {
        encoding: "utf8",
        flag: "a" // flag that specifies that it will append stuff
    },
    function(){ console.log("done!") }
)
like image 45
Robert Sinclair Avatar answered Nov 17 '22 07:11

Robert Sinclair