how to write file with node webkit js?

ぃ、小莉子 提交于 2020-03-05 12:50:07

问题


When I build my app, 'fs' module doesn't work. So, it must write file, but when I start my app nothing happens. But if I start my app with:

$ /path/to/app nw

It works correctly. What's wrong?

Some code, where I use fs:

function check_prob_2(){
    console.log('Problem 2');
    fs.appendFile('log.txt', 'Checking problem 2: \n\n');
    ...
    }

I use that function, but it doesn't work. It doesn't work only after build application. I build it with this guide


回答1:


Try this:

Include the following (standard) module:

var path = require('path');

Specify the path as follows:

fs.appendFile(path.resolve(__dirname, './log.txt'), 'Checking problem 2: \n\n');

More info on the __dirname global can be found here.

EDIT

Since __dirname is not defined in node-webkit, you'll have to use the following workaround:

Make a file util.js or however you want to call it, containing this line:

exports.dirname = __dirname;

The __dirname variable can now be exposed in your main file:

var dirname = require('./util.js').dirname;

And replace __dirname by dirname in the code.

Details here



来源:https://stackoverflow.com/questions/32693835/how-to-write-file-with-node-webkit-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!