Skip to content
Advertisement

Webpack : Cannot read property ‘readFile’ of undefined, No output files

Using webpack > 5 version. Below is my appDevMiddleware.jscongifuration

JavaScript

When i do npm start of my React App, i’m getting

JavaScript

This is exactly showing here

JavaScript

I will have to somehow use the promises for readFile which is something like const { readFile } = require('fs').promises

How should i replace const fs = middleware.fileSystem; with promise for this issue?

Advertisement

Answer

webpack-dev-middleware uses memfs as its default outputFileSystem. WDM didn’t expose the API so that you can NOT get the outputFileSystem from the instance of WDM. That’s why you got that error.

You should create your own outputfilesystem, you can use the memfs to do that explicitly. Then, you can get the index.html from the memory file system.

You can use util.promisify() method from util Node.js built-in module to promisify the fs.readFile method.

E.g.

webpack.config.js:

JavaScript

app.js:

JavaScript

Start the server:

JavaScript

Access http://localhost:3000/test:

JavaScript

package version:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement