Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 网页包热中间件不工作_Javascript_Node.js_Express_Webpack - Fatal编程技术网

Javascript 网页包热中间件不工作

Javascript 网页包热中间件不工作,javascript,node.js,express,webpack,Javascript,Node.js,Express,Webpack,我是webpack的新手,我正在尝试在ejs中使用express时创建一个实时服务器。我被告知使用webpack中间件hot,但当我尝试运行我的服务器时,我得到以下错误 fs.js:1649 binding.lstat(baseLong); ^ Error: ENOENT: no such file or directory, lstat 'C:\index.js' at Object.realpathSync (fs.js:1649:15)

我是webpack的新手,我正在尝试在ejs中使用express时创建一个实时服务器。我被告知使用webpack中间件hot,但当我尝试运行我的服务器时,我得到以下错误

fs.js:1649
      binding.lstat(baseLong);
              ^
Error: ENOENT: no such file or directory, lstat 'C:\index.js'
    at Object.realpathSync (fs.js:1649:15)
    at Object.eval (webpack:///./node_modules/mini-css-extract-plugin/dist/index.js?:28:48)
    at eval (webpack:///./node_modules/mini-css-extract-plugin/dist/index.js?:329:30)
    at Object../node_modules/mini-css-extract-plugin/dist/index.js (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:9092:1)
    at __webpack_require__ (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:20:30)
    at eval (webpack:///./node_modules/mini-css-extract-plugin/dist/cjs.js?:3:18)
    at Object../node_modules/mini-css-extract-plugin/dist/cjs.js (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:9080:1)
    at __webpack_require__ (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:20:30)
    at Object.eval (webpack:///./webpack.config.js?:4:30)
    at eval (webpack:///./webpack.config.js?:139:30)
在server.js(我称之为app.js)中,似乎需要webpack,而webpack.config是导致问题的原因

webpack.config.js

const webpack = require("webpack");
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = [{
    name: 'server',
    entry: ['./src/server/app.js'],
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'dist/server'),
        filename: 'app.js'
    },
    plugins: [
        new CopyWebpackPlugin([
            {from: 'src/server/views', to: 'views'}
        ])
    ]
},
{
    name: 'client',
    entry: ['babel-polyfill', "webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000", './src/client/js/index.js'],
    output: {
        path: path.resolve(__dirname, 'dist/client'),
        filename: 'js/bundle.js',
        publicPath: "/"
    },
    devServer: {
        contentBase: './dist'
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.css',
        }),
        new CopyWebpackPlugin([
            {from: 'src/client/img', to: 'img'}
        ]),
        new CleanWebpackPlugin('dist'),
        new webpack.HotModuleReplacementPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.s?[ac]ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    { loader: 'css-loader', options: { url: false, sourceMap: true } },
                    { loader: 'sass-loader', options: { sourceMap: true } }
                ]
            },
            {
                test   : /\.(png|jpg|jpeg|gif?)(\?[a-z0-9=&.]+)?$/,
                loader : 'url-loader',
                options: {
                    limit: 100000
                }
            }
        ]
    }
}];
var express = require('express'),
    app     = express(),
    path = require('path'),
    axios   = require('axios'),
    ejs     = require("ejs").__express;

var webpack = require('webpack'),
    webpackConfig = require('../../webpack.config'),
    compiler = webpack(webpackConfig);

// I commented these lines out while trying to find the issue, it looks like it is the line above that is the culprit
// app.use(require('webpack-dev-middleware')(compiler, {
//         noInfo: true,
//         publicPath: webpackConfig.output.publicPath
//     })
// );

// app.use(require('webpack-hot-middleware')(compiler));

app.set("view engine", "ejs");
app.set("views", "dist/server/views")
app.engine('.ejs', ejs);
app.use(express.static("dist/client"));

app.listen(8080, 'localhost', () => {
    console.log('The server has started');
});
server.js

const webpack = require("webpack");
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = [{
    name: 'server',
    entry: ['./src/server/app.js'],
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'dist/server'),
        filename: 'app.js'
    },
    plugins: [
        new CopyWebpackPlugin([
            {from: 'src/server/views', to: 'views'}
        ])
    ]
},
{
    name: 'client',
    entry: ['babel-polyfill', "webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000", './src/client/js/index.js'],
    output: {
        path: path.resolve(__dirname, 'dist/client'),
        filename: 'js/bundle.js',
        publicPath: "/"
    },
    devServer: {
        contentBase: './dist'
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.css',
        }),
        new CopyWebpackPlugin([
            {from: 'src/client/img', to: 'img'}
        ]),
        new CleanWebpackPlugin('dist'),
        new webpack.HotModuleReplacementPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.s?[ac]ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    { loader: 'css-loader', options: { url: false, sourceMap: true } },
                    { loader: 'sass-loader', options: { sourceMap: true } }
                ]
            },
            {
                test   : /\.(png|jpg|jpeg|gif?)(\?[a-z0-9=&.]+)?$/,
                loader : 'url-loader',
                options: {
                    limit: 100000
                }
            }
        ]
    }
}];
var express = require('express'),
    app     = express(),
    path = require('path'),
    axios   = require('axios'),
    ejs     = require("ejs").__express;

var webpack = require('webpack'),
    webpackConfig = require('../../webpack.config'),
    compiler = webpack(webpackConfig);

// I commented these lines out while trying to find the issue, it looks like it is the line above that is the culprit
// app.use(require('webpack-dev-middleware')(compiler, {
//         noInfo: true,
//         publicPath: webpackConfig.output.publicPath
//     })
// );

// app.use(require('webpack-hot-middleware')(compiler));

app.set("view engine", "ejs");
app.set("views", "dist/server/views")
app.engine('.ejs', ejs);
app.use(express.static("dist/client"));

app.listen(8080, 'localhost', () => {
    console.log('The server has started');
});
我的文件结构是

project/
└── src/
    ├── client/
    │   ├── img/
    │   ├── js/
    │   └── styles/
    └── server/
        ├── views
        └── app.js

你能粘贴你的index.js以便我们能看到path@samnupel我的index.js中没有任何内容。里面应该有什么?