如何用node webkit js写文件?
how to write file with node webkit js?
当我构建我的应用程序时,'fs' 模块不起作用。所以,它必须写入文件,但是当我启动我的应用程序时,什么也没有发生。但是如果我用以下命令启动我的应用程序:
$ /path/to/app nw
它工作正常。怎么了?
一些代码,我使用 fs:
function check_prob_2(){
console.log('Problem 2');
fs.appendFile('log.txt', 'Checking problem 2: \n\n');
...
}
我使用了那个功能,但是它不起作用。它仅在构建应用程序后才起作用。我用 this guide
构建它
试试这个:
包括以下(标准)模块:
var path = require('path');
指定路径如下:
fs.appendFile(path.resolve(__dirname, './log.txt'), 'Checking problem 2: \n\n');
有关 __dirname 全局的更多信息,请参见 here。
编辑
由于 __dirname 未在 node-webkit 中定义,您必须使用以下解决方法:
创建一个文件 util.js
或者你想怎么称呼它,包含这一行:
exports.dirname = __dirname;
现在可以在您的主文件中公开 __dirname 变量:
var dirname = require('./util.js').dirname;
并将代码中的__dirname
替换为dirname
。
详情here
当我构建我的应用程序时,'fs' 模块不起作用。所以,它必须写入文件,但是当我启动我的应用程序时,什么也没有发生。但是如果我用以下命令启动我的应用程序:
$ /path/to/app nw
它工作正常。怎么了?
一些代码,我使用 fs:
function check_prob_2(){
console.log('Problem 2');
fs.appendFile('log.txt', 'Checking problem 2: \n\n');
...
}
我使用了那个功能,但是它不起作用。它仅在构建应用程序后才起作用。我用 this guide
构建它试试这个:
包括以下(标准)模块:
var path = require('path');
指定路径如下:
fs.appendFile(path.resolve(__dirname, './log.txt'), 'Checking problem 2: \n\n');
有关 __dirname 全局的更多信息,请参见 here。
编辑
由于 __dirname 未在 node-webkit 中定义,您必须使用以下解决方法:
创建一个文件 util.js
或者你想怎么称呼它,包含这一行:
exports.dirname = __dirname;
现在可以在您的主文件中公开 __dirname 变量:
var dirname = require('./util.js').dirname;
并将代码中的__dirname
替换为dirname
。
详情here