问题
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