javascript - Node JS fs module inside browser -
i have scenario want export data csv client-side. have textbox/area or whatever user can input data , ideally 1 click, local csv file updated data.
this achievable nodejs server interaction, , core modules (specifically fs
module), apparently not browser.
i found out node modules (for example underscore
), support requirejs's method of making particular module work in browser. underscore 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>
the above works fine , show alert: 4.
but when try same fs
module won't work. show error:
module name "util" has not been loaded yet context: _. use require([])
as far can understand, because fs
requires several other modules, 1 of them being util
.
so proceeded add these modules requirejs config. still no luck, tested util
module doesn't seem require other modules work.
and stuck on error: uncaught referenceerror: exports not defined
i tried modularizing util
module encapsulating whole module source code in this:
define([], function() {})
but didn't work either... i've tried copying underscore
's model still no luck.
so wondering if managed use util
& fs
modules (or other core nodejs modules) within browser libraries such requirejs or browserify.
that's right, exports
node-specific js (used make part of module available outside module) , isn't supported web-browsers. though nodejs technically js, there client specific properties (like window
property browsers, , exports
nodejs apps) can't interchanged.
that said, here's client side js answer csv problem.
Comments
Post a Comment