Require fs module in the project, using webpack, but always reporting errors?

require the fs module in the front-end project, but keep reporting errors?

fs.readFileSync is not a function

introduce code

var fs = require("fs");
fs.readFileSync("index.html","utf8");

this is the webpack configuration file

const path = require("path") //  "path" 
const HTMLPlugin = require("html-webpack-plugin")
const webpack = require("webpack");

const config = {
    // 
    entry: {
        app: path.join(__dirname, "../src/app.js")  // app.js
    },
    // 
    output: {
        filename: "[name].[hash].js",  //nameentry; hash apphashhash
        path: path.join(__dirname, "../dist"), // 
        publicPath: "" // 
    },
    // loader
    module: {
        rules: [
            {
                test: /.jsx$/, //loader.jsx
                loader: "babel-loader"
            },
            {
                test: /.(js)$/, //loader.js
                loader: "babel-loader",
                exclude: [
                    path.join(__dirname, "../node_modules")  // node_modulesbabeljs
                ]
            }
        ]
    },
    node: {
      fs: "empty"
    },
    plugins: [
        new HTMLPlugin({
            template: path.join(__dirname, "../src/template.html") // template.htmlhtml
        }),
        new webpack.HotModuleReplacementPlugin()
    ]
}

const isDev = process.env.NODE_ENV === "development"

if (isDev) {
    config.devServer = {
        host: "0.0.0.0",  // 127.0.0.1localhost, ip
        port: "8888",
        contentBase: path.join(__dirname, "../dist"),
        hot: true,  //
        overlay: {  // 
            errors: true //error
        },
        // output
        publicPath: "",  // /public
        historyApiFallback: {
            index: "/index.html" // 404url
        }
    }
}

module.exports = config
Feb.27,2021

look that you have an anguler tag. If your fs logic is written in business code (running in a pure browser), it won't work.


I have the same problem. I wonder if the subject has solved it now.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e96d65-473e4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e96d65-473e4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?