简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM