Can';将Node.js服务器与Web包绑定时找不到index.html

Can';将Node.js服务器与Web包绑定时找不到index.html,node.js,express,webpack,Node.js,Express,Webpack,我正在尝试设置webpack以捆绑我的后端代码 我的网页包配置如下所示: const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const nodeExternals = require('webpack-node-externals'); const o

我正在尝试设置webpack以捆绑我的后端代码

我的网页包配置如下所示:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

const outputDirectory = 'dist';

const client = {
  mode: 'production',
  entry: {
    'app': [
      'babel-polyfill',
      './client/index.js'
    ]
  },
  output: {
    path: path.join(__dirname, outputDirectory),
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    rules: [
      { 
        test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(gif|svg|jpg|png)$/,
        loader: "file-loader"
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin([outputDirectory]),
    new HtmlWebpackPlugin({
      template: './index.html',
    })
  ]
}

const server = {
  mode: 'production',
  target: 'node',
  entry: {
    'app': [
      './server/server.js'
    ]
  },
  externals: [nodeExternals()],
  output: {
    path: path.join(__dirname, '/server'),
    filename: 'server.bundle.js'
  }
}

module.exports = [client, server]
如果我运行非webpack server.js,一切正常。但是,如果我运行webpack bundled server.bundle.js,express会抛出:

错误:enoint:没有这样的文件或目录,stat'/dist/index.html'


两个服务器文件位于同一目录中。以前有人遇到过这个问题吗?

我发现了,webpack的文档中没有明确说明,但在使用express时,您需要配置“节点”属性

把这个添加到你的配置中

  node: {
    // Need this when working with express, otherwise the build fails
    __dirname: false,   // if you don't put this is, __dirname
    __filename: false,  // and __filename return blank or /
  },

“我能看看你的客户端配置吗?”AliDoustkani补充道