38

While executing ng build --prod --base-href ./ for building my cordova app, the final output throws an error as below.

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

I did end up fixing this by changing type module to text/javascript

src="runtime-es2015.858f8dd898b75fe86926.js" type="module">

src="runtime-es2015.858f8dd898b75fe86926.js" type="text/javascript">

Is there something that can be done within angular.json file to fix or am I missing out something here?

Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48

7 Answers7

21

I had the same issue.

  1. Created a new project.
  2. Built a production version

      "build-production": "ng build --configuration=production --extract-css=false --prod --aot"
    
  3. Deployed to NGINX

  4. White page with no content between tags in Chrome Element inspector

Fix

Update tsconfig.json

        "target": "es5",

Then rebuild the application and deploy again.

This solution worked for me, I now have content in my deployed app.

Hope it helps somebody

englishPete
  • 809
  • 10
  • 15
10

I have this same issue (similar) when creating an angular / electron app.

I follow the steps here:

https://alligator.io/angular/electron/

and I just get a blank (white) screen when I run the electron app. When you inspect the app with dev tools, you get a handful of error messages in the console like:

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

These appear on all the JS includes that are present in the dist/index.html file.

I have to manually go through all the script tags (like this):

<script src="runtime-es2015.858f8dd898b75fe86926.js" type="module">

and change them to include a mime type:

<script src="runtime-es2015.858f8dd898b75fe86926.js" type="text/javascript">

Only then does it work inside the electron window. If I run project using "ng serve", though, and look at the webpage served up by angular, then it works just fine.

I think it's something to do with files being loaded locally from the file system and not providing a mime type, while when they are served from a webserver, a mime type is provided.

adamdport
  • 11,687
  • 14
  • 69
  • 91
John
  • 109
  • 3
8

You must change the property "target" in the file "tsconfig.json" to "es5". Read this blog entry, "Differential Loading by Default":

https://blog.angular.io/version-8-of-angular-smaller-bundles-cli-apis-and-alignment-with-the-ecosystem-af0261112a27

This property chooses between modern or legacy JavaScript based on the browser capabilities:

<script type="module" src="…"> // Modern JS

<script nomodule src="…"> // Legacy JS

Ameen Rashad
  • 514
  • 6
  • 17
  • 13
    While this technically resolves the error, it's equivalent in quality to saying "just don't use angular 8". It's not the answer most of us are looking for. – adamdport Aug 29 '19 at 17:01
2

Never used Cordova, but encountered the same issue with Nginx. The solution I implemented (without the regression proposed by accepted solution): add "module" to MIME types. See related question

1

I managed to get my Electron application working (with Angular 8) by modifying the browserlist file within the root directory. As with your post, I too was having issues with Mime Types.

I added Chrome >= 70 and Chrome <= 72 to the file given the latest Chromium instance in Electron is 72. Seemed to do the trick.

Edit: I do realize you are using Cordova and I don't quite know what that is built on (e.g. Chromium). In this case, try modifying your browserlist to reflect earlier versions of browsers. You might find the queries necessary to achieve this here: https://github.com/browserslist/browserslist

Hope this helps. Gave me quite the headache.

1

Had the same issue not with Cordova but with an express app. It turned out when I did the ng build in angular 8, it created the files under a new directory with the app name. So when I ran ng build I got dist/<app-name>.

When I tried to run the app in the express server I only updated the path to my index.html. For eg:

app.get('*', (req, res) => {
   res.sendFile(path.join(__dirname, 'dist/<app-name>/index.html'));
});

and my index.html had these lines <script src="runtime-es2015.js" type="module"></script> which again tried to load the index.html instead of the .js files and hence I got the error.

The fix that I did was to update the location of the public directory which served my javascript and css files . For eg:

app.use(express.static(path.join(__dirname, 'dist/<app-name>')));
ecosystem31
  • 276
  • 2
  • 4
1

This issue has be solved in your proxy.

Therefore the question is, what kind of proxy are you using?

If you are using Nginx. You have to configure the Nginx proxy like this:

http {
    types {
      module js;
    }
    include       /etc/nginx/mime.types;
    # rest of your config...
}

Found the solution here.

R. de Ruijter
  • 513
  • 5
  • 13