如何用node webkit js写文件? [英] how to write file with node webkit js?

查看:101
本文介绍了如何用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');
    ...
    }

我使用了该功能,但是不起作用.它仅在构建应用程序后才起作用.我使用本指南 >

解决方案

尝试一下:

包括以下(标准)模块:

var path = require('path');

指定如下路径:

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

可以在此处找到有关__dirname全局的更多信息./p>

编辑

由于在node-webkit中未定义__dirname,因此您必须使用以下解决方法:

制作一个文件util.js,或者您想调用它,但包含以下行:

exports.dirname = __dirname;

__ dirname变量现在可以在您的主文件中显示:

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

并在代码中将__dirname替换为dirname.

详细信息此处

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

解决方案

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

这篇关于如何用node webkit js写文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆