[Fixed] I have this error saying something about failed to load module scripts – Javascript

by
Alexei Petrov
html webpack

Quick Fix: To resolve this issue, you can use the "html-bundler-webpack-plugin". It allows you to directly specify the source JS file path within the HTML, and the plugin will compile and bundle the necessary JavaScript and CSS files.

The Problem:

A developer is using webpack as a module bundler, React for the frontend, and Node.js for the backend. They are encountering an error in the console that says, "Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.". The code they provided includes a webpack configuration and an HTML file with a script tag. They have tried changing the script type to "text/javascript" but that didn’t render anything and still didn’t resolve the error. They are seeking assistance in resolving this issue.

The Solutions:

Solution 1: Use the “html-bundler-webpack-plugin” to compile source files directly in HTML

  1. Install the Plugin:
    Using npm install html-bundler-webpack-plugin --save-dev, install the html-bundler-webpack-plugin.

  2. Specify Source JS File in HTML:
    Modify the HTML file and specify the source JS file using a relative path to the HTML file. For example:

     <!DOCTYPE html>
     <html lang="en">
       <head>
         <meta charset="UTF-8" />
         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
         <title>Document</title>
         <!-- specify source JS file using a relative path to the HTML file -->
         <script type="module" src="./App/Main/index.jsx"></script>
       </head>
       <body></body>
     </html>
    
  3. Add Plugin to Webpack Config:
    Add the html-bundler-webpack-plugin to the webpack configuration. For example:

    import HtmlBundlerPlugin from 'html-bundler-webpack-plugin';
    
    export default {
      output: {
        path: path.resolve('./dist'),
      },
      mode: 'production',
      plugins: [
        new HtmlBundlerPlugin({
          entry: {
            // define templates here
            index: './App/index.html', // <= path to HTML file
          },
          js: {
            // output filename of compiled JavaScript
            filename: 'js/[name].[contenthash:8].js',
          },
          css: {
            // output filename of extracted CSS
            filename: 'css/[name].[contenthash:8].css',
            //inline: true, // inlines CSS into HTML
          },
        }),
      ],
      // ... other webpack config
    };
    
  4. Run Webpack:
    Run webpack to generate the compiled HTML file. For example, use webpack or webpack --mode production to build the production version.

  5. Output:
    The generated HTML file will be placed in the specified output.path directory, with compiled JS and CSS files referenced in the <head> section.

Q&A

How to solve error-Failed to load module script: Expected a JavaScript module script?

You can use the "html-bundler-webpack-plugin" to compile style, script, images source files used in HTML.

How to add html-bundler-webpack-plugin to webpack config?

Add the plugin to webpack config, write the js, css output filename here.

How to write the entrypoint.html file?

Add the plugin to webpack config, write the js, css output filename here.

Video Explanation:

The following video, titled "How to fix Unexpected Token in JSON error (for web developers ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.