Writing files in Node.js

前端 未结 19 2017
情歌与酒
情歌与酒 2020-11-21 11:57

I\'ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

相关标签:
19条回答
  • 2020-11-21 12:33

    I liked Index of ./articles/file-system.

    It worked for me.

    See also How do I write files in node.js?.

    fs = require('fs');
    fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
        if (err) 
            return console.log(err);
        console.log('Wrote Hello World in file helloworld.txt, just check it');
    });
    

    Contents of helloworld.txt:

    Hello World!
    

    Update:
    As in Linux node write in current directory , it seems in some others don't, so I add this comment just in case :
    Using this ROOT_APP_PATH = fs.realpathSync('.'); console.log(ROOT_APP_PATH); to get where the file is written.

    0 讨论(0)
提交回复
热议问题