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

查看:118
本文介绍了浏览器中的 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 &浏览器中的 fs 模块(或任何其他核心 NodeJS 模块)以及 RequireJS 或 Browserify 等库.

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天全站免登陆