Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use node.js fs with Webpack

I'm doing the following in a js file

    var FS = require('fs');
    var TestGoogle = eval(FS.readFileSync('../loader.js', 'utf8'));

I added to my webpack config:

node: {
  fs: "empty"
}

The webpack compiles with Babel but I get an error in the js console

FS.readFileSync is not a function

Why is this? I would simply like to import a javascript file. Specifically in order to use material bar charts of google. https://developers.google.com/chart/interactive/docs/gallery/barchart#creating-material-bar-charts

In vanilla js, I need to

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load('current', {'packages':['bar']});

And then, as the tutorial states, use google.charts.Bar instead of google.visualization.BarChart. Unfortunately, I can't get this simple vanilla JS to play nicely with Node.js, Babel, Webpack.

like image 753
user3426600 Avatar asked Apr 18 '16 12:04

user3426600


People also ask

Can you use webpack for node?

Webpack provides a Node. js API which can be used directly in Node. js runtime.

Do I need to install fs in node JS?

There is no need to install fs in node js to use it.

Can you use fs in browser?

Working with system files technically works on all modern browsers.


1 Answers

Try add target: 'node' to webpack.config.js. I had the same problem and it solved for me.

like image 97
fjozsef Avatar answered Sep 22 '22 15:09

fjozsef