3

When I'm running npm run build, it is not creating index.html in the dist/ directory. The reason I need index.html is that I want to deploy my Vue project to AWS EC2 (/var/www/html/). What to do to generate this index.html?

My dist/ directory structure after running npm run build:

enter image description here

My package.json:

{
  "name": "proto",
  "description": "Prototype",
  "version": "1.0.0",
  "license": "MIT",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "@melmacaluso/vue-modal": "^2.1.0",
    "firebase": "^7.14.2",
    "fusioncharts": "^3.15.1-sr.1",
    "vue": "^2.5.11",
    "vue-fusioncharts": "^3.0.4",
    "vue-router": "^3.1.6",
    "vuex": "^3.3.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-stage-3": "^6.24.1",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.4",
    "vue-loader": "^13.0.5",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  }
}

My webpack.config.js:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

The build command output:

$ npm run build

> [email protected] build C:\Users\john\Documents\VUE\dummy_prototype_1
> cross-env NODE_ENV=production webpack --progress --hide-modules
3
  • 1
    depoy in /var/www/html/ index.html file and folder /dist (with bundles), and be sure that in index.html there is a <script> line that loads bundle Commented Sep 14, 2020 at 11:30
  • Thanks for the information. Since I've already shared the dist/ folder, can you give more idea of the <script> line? Pardon me, I'm new to this. Commented Sep 14, 2020 at 11:34
  • 1
    <script src="./dist/your_web.min.bundle.js"></script> Commented Sep 14, 2020 at 11:43

2 Answers 2

4
+50

In your webpack.config.js, scroll down then add the HtmlWebpackPlugin plugin into module.exports.plugins as @Michal Levy suggested.

Updated: For the error (webpack.js:348 throw err; TypeError: Cannot read property 'make' of undefined), one possible reason is version issue. The solution is downgrade (uninstall then install) html-webpack-plugin to 3.2.0.

Below is new configuration for plugins:

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    // below is the plugin you need to add
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, './dist/index.html'), // the path where is the `index.html` generated.
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}
Sign up to request clarification or add additional context in comments.

4 Comments

After copying the above code to webpack.config.js, when I'm running npm run build, I'm getting this error: C:\Users\...\node_modules\webpack\bin\webpack.js:348 throw err; TypeError: Cannot read property 'make' of undefined
Did you install the plugin? npm install --save-dev html-webpack-plugin?
Downgrade (uninstall then install) html-webpack-plugin to 3.2.0, then try again.
Thank you very much, it worked. You are either a magician or a genius.
2

Just use HtmlWebpackPlugin

It will automatically create (new or from template you provide) index.html in the dist folder and inject any necessary <script> (for js bundles) or <link> (for extracted CSS) tags

As it seems you already have index.html template, use template option - see the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.