首页 > 解决方案 > 如何修复节点文件系统写入功能?

问题描述

函数 writeFile 已识别,但在 ubuntu 18.04 中不起作用;但是,它确实适用于我的本地环境。

但它不写。

我为该文件夹添加了完全权限。

const fs = require('fs');

fs.writeFile( 'test.json', 'dfsdfsd', function(err){
        if (err) {
                return console.log(err);
        }
        console.log('data appended');
        console.log(fs.writeFile);
});


结果

root@laravel-dropin:/var/www/html/resources# node js/test.js 
data appended
[Function: writeFile]

标签: node.jsubuntu-18.04

解决方案


答案:这可以代替 writeFile 函数


let writeStream = fs.createWriteStream('secret.json');

        // write some data with a base64 encoding
        writeStream.write(s);

        // the finish event is emitted when all data has been flushed from the stream
        writeStream.on('finish', () => {  
            console.log('wrote all data to file');
        });

        // close the stream
        writeStream.end();  


推荐阅读