【发布时间】:2021-08-02 08:58:51
【问题描述】:
使用webpack > 5 版本。下面是我的appDevMiddleware.jscongifuration
const path = require('path');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
function createWebpackMiddleware(compiler, publicPath) {
return webpackDevMiddleware(compiler, {
// noInfo: true,
publicPath,
// silent: true,
stats: 'errors-only',
});
}
module.exports = function addDevMiddlewares(app, webpackConfig) {
const compiler = webpack(webpackConfig);
const middleware = createWebpackMiddleware(compiler, webpackConfig.output.publicPath);
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
// Since webpackDevMiddleware uses memory-fs internally to store build
// artifacts, we use it instead
const fs = middleware.fileSystem;
app.get('*', (req, res) => {
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
res.sendStatus(404);
} else {
res.send(file.toString());
}
});
});
};
当我对我的 React 应用程序执行 npm start 时,我得到了
TypeError: Cannot read property 'readFile' of undefined
at D:\master-instore-dashboard\server\middlewares\addDevMiddlewares.js:29:8
这正是在这里显示
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
我将不得不以某种方式将promises 用于readFile,类似于const { readFile } = require('fs').promises
对于这个问题,我应该如何用 promise 替换 const fs = middleware.fileSystem;?
【问题讨论】:
标签: javascript node.js reactjs webpack