Using Webpack with React-router bundle.js Not Found

后端 未结 3 2036
南旧
南旧 2021-01-04 01:54

I build a project with Webpack and react-rounter. this is my code:

相关标签:
3条回答
  • 2021-01-04 02:25

    You are using a relative path to describe path of the bundle.js in your index.html.

    You should use absolute path for bundle.js in your index.html

    Absolute path contains the root directory and all other subdirectories in which a file or folder is contained.

    If it's in the same path with your index.html.

    eg.

    public/index.html

    public/bundle.js

    This would solve your problem.

    <script src="/bundle.js"></script>
    
    0 讨论(0)
  • 2021-01-04 02:31

    If you happen to be using the HtmlWebpackPlugin editing directly index.html is not an option. So, to fix this particular issue, add publicPath and specify / as the root folder inside webpack.config.js:

    const path = require('path')
    
    module.exports = {
      entry: './app/index.js',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/'
      },
      // more config stuff goes below..
    } 
    

    Don't forget to restart the webpack dev server for these changes to take effect

    More info about publicPath here.

    0 讨论(0)
  • 2021-01-04 02:38

    Adding <base href="/" /> to head tag of my index.html file before including any scripts worked for me.

    0 讨论(0)
提交回复
热议问题