javascript 浏览器中的 Node JS fs 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27019242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:50:31  来源:igfitidea点击:

Node JS fs module inside browser

javascriptnode.jsmodulerequirejsbrowserify

提问by magicode118

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.

我有一个场景,我想从客户端将数据导出到 CSV。我将有一个文本框/区域或任何用户可以输入数据的地方,然后理想情况下只需单击一下,本地 CSV 文件将使用该数据进行更新。

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

使用带有服务器交互的 NodeJS 及其核心模块(特别是fs模块)可以轻松实现这一点,但显然不能通过浏览器实现。

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:

我发现某些节点模块(例如underscore)支持 RequireJS 使特定模块在浏览器中工作的方法。所以为了下划线,我这样做了:

methods.js

方法.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

常见的.js

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

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

index.html

索引.html

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

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

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

以上工作正常,并会显示一个警报:4。

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

但是当我对fs模块尝试同样的操作时,它就行不通了。它会显示这个错误:

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

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

据我所知,这是因为fs需要其他几个模块,其中之一是util.

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

所以我继续将所有这些模块添加到 RequireJS 配置中。但仍然没有运气,所以我专门测试了util模块本身,因为这似乎不需要其他模块工作。

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

现在我被这个错误困住了: Uncaught ReferenceError: exports is not defined

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

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

define([], function() {})

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

但它也没有工作......我也试过 copyingunderscore的模型,但仍然没有运气。

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

所以我想知道是否有人设法在浏览器中使用util&fs模块(或任何其他核心 NodeJS 模块)和 RequireJS 或 Browserify 等库。

采纳答案by FloatingRock

That's right, exportsis 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 windowproperty for browsers, and exportsfor NodeJS apps) that can't be interchanged.

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

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

也就是说,是 CSV 问题的客户端 JS 答案。

回答by FredyC

Your best bet (and probably only one) is to use HTML5 FileSystem API. I am not aware of any other browser feature that would allow to work with files on client computer except maybe Flash and similar solution.

您最好的选择(可能只有一个)是使用HTML5 FileSystem API。我不知道有任何其他浏览器功能可以处理客户端计算机上的文件,除了 Flash 和类似的解决方案。

I am bit confused by your browserify tag since you are not obviously using Browserify. That would fix your issue with "exports is not defined".

我对你的 browserify 标签有点困惑,因为你显然没有使用 Browserify。这将解决“未定义导出”的问题。