Read a file at resolved path in Webpack

Webpack bundles js code for frontend usage. Browser does not have fs module, and it can't do readFile. You can convert template.css to valid js and require it with webpack, or use some webpack loader to do such conversion on-the-fly.


I had to dig deep to find this one.

It seems it can be solved by using some browserify plugins along with webpack.

The answer below is a copy-paste since SO does not allow links-only answers

Say you have the following Node source:

var test = require('fs').readFileSync('./test.txt', 'utf8');

After npm install transform-loader brfs --save, add the following loader to your config:

module.exports = {
    context: __dirname,
    entry: "./index.js",
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: "transform?brfs"
            }
        ]
    }
}

And here's your link: https://github.com/webpack/transform-loader#typical-brfs-example