浏览器中的Node JS fs模块 [英] Node JS fs module inside browser

查看:655
本文介绍了浏览器中的Node JS fs模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想从客户端将数据导出到CSV.我将拥有一个文本框/区域,或用户可以在其中输入数据的任何区域,然后理想情况下,只需单击一下,便会使用该数据更新本地CSV文件.

I have a scenario where I want to export data to CSV from the client-side. I will have a textbox/area or whatever where the user can input data and then ideally with one click, a local CSV file will be updated with that data.

使用NodeJS与服务器交互及其核心模块(特别是fs模块)可以轻松实现这一点,但显然不是从浏览器中获得的.

This is easily achievable with NodeJS with server interaction, and its core modules (specifically fs module), but apparently not so much from a browser.

我发现某些节点模块(例如underscore)支持RequireJS的使特定模块在浏览器中工作的方法.因此,对于下划线,我这样做:

I found out that certain node modules (for example underscore), support RequireJS's method of making a particular module work in the browser. So for underscore I did this:

methods.js

define(['underscore'],function(_) {

    var Methods = {
        doSomething: function() {

            var x = _.size({one: 1, two: 2, three: 3, xuz: 3});

            alert(x);
        }
    };

    return Methods;
});

common.js

requirejs.config({
    baseURL: 'node_modules',
    paths: {
        underscore: 'underscore/underscore',
    }
});

require(['methods'], function(y){
    y.doSomething();
});

index.html

<script data-main="common" src="require.js"></script>
<script>
require(['common'], function() {

    require(['methods.js']);
});
</script>

上面的方法工作正常,并且会显示警报:4.

The above works fine and will show an alert: 4.

但是当我对fs模块尝试相同的操作时,它将无法正常工作.它将显示此错误:

But when I try the same with the fs module it won't work. It will show this error:

Module name "util" has not been loaded yet for context: _. Use require([])

据我了解,这是因为fs还需要其他几个模块,其中一个是util.

As far as I can understand, this is because fs requires several other modules, one of them being util.

所以我继续将所有这些模块添加到RequireJS配置中.但是仍然没有运气,所以我专门对util模块进行了单独测试,因为这似乎不需要其他模块即可工作.

So I proceeded to add all these modules to the RequireJS config. But still no luck, so I specifically tested util module by itself as this doesn't seem to require other modules to work.

现在我陷入了这个错误:Uncaught ReferenceError: exports is not defined

And now I am stuck on this error: Uncaught ReferenceError: exports is not defined

我尝试通过将整个模块源代码封装在此模块中来模块化此util模块:

I tried modularizing this util module by encapsulating the whole module source code in this:

define([], function() {})

但是它也不起作用...我也尝试过复制underscore的模型,但是仍然没有运气.

But it didn't work either... I've also tried copying underscore's model but still no luck.

所以我想知道是否有人设法使用util&使用诸如RequireJS或Browserify之类的库的浏览器中的fs模块(或任何其他核心NodeJS核心模块).

So I was wondering if anyone managed to use util & fs modules (or any other core NodeJS modules) within the browser with libraries such as RequireJS or Browserify.

推荐答案

是的,exports是特定于节点的JS(用于使模块的一部分在模块外部可用),并且不受网络浏览器的支持.即使从技术上讲NodeJS是JS,也有不能互换的客户端特定属性(如浏览器的window属性和NodeJS应用程序的exports).

That's right, exports is node-specific JS (used to make part of your module available outside the module) and isn't supported by web-browsers. Even though NodeJS is technically JS, there are client specific properties (like the window property for browsers, and exports for NodeJS apps) that can't be interchanged.

也就是说,这是客户端JS回答CSV问题.

That said, here's a client side JS answer to the CSV problem.

这篇关于浏览器中的Node JS fs模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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