Express routing static file: Failed to load module script

Lewis Morgans

I'm serving my Angular 9 application the same way I always have, and this is the first time I'm experiencing this issue.

I keep getting the following error: "Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html".

I've scoured the net for answers and can't seem to find anything that fixes it. Everything I have done seems to adhere to the advice given. I'm wondering if this is a new issue with Angular v9?

My express app.js is located in another folder to my Angular project. Below is my folder structure:

  • client

    • dist
      • index.html
  • middle-tier

    • app.js

app.js:

app.use(express.static(__dirname + '/dist'));

app.get('*/', (req, res) => { 
    const indexFile = path.join(__dirname, '../client/dist/index.html');
    res.sendFile(indexFile);
})

The file path is correct, I've consoled logged it and it all adds up. I changed the output path in the angular.json also to just dist instead of having another subfolder.

I still get this error. Can anyone help?

Sebastian Kaczmarek

You have to use express.static() for that:

app.use(express.static(path.join(__dirname, '../client/dist')))

The issue here is that you always respond with index.html file and if the file contains some <script> tags then the browser would expect to receive a .js file but your server responds with a .html file and that's why you get the error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS routing to static file

From Dev

Vue-resource can't load express static file

From Dev

Simple redirect in Express with static file

From Dev

Cannot load the script with the type "module"

From Dev

Express routing from configuration file

From Dev

Haskell Failed to load interface for module

From Dev

express static file serve configuration

From Dev

DLL load failed: The specified module could not be found. File "<stdin>", line 1, in <module>

From Dev

Electron Failed to load Module

From Dev

How to fix: "Failed to load module script ..." Angular 8, Electron 5

From Dev

Failed to load module script because of a disallowed MIME type

From Dev

Can run script from pycharm, but not from Terminal. ImportError: DLL load failed: The specified module could not be found

From Dev

Express middleware for express.static() not routing correctly and throwing next() is not a function

From Java

Express File Serving and Static Files

From Dev

Failed to load module "canberra-gtk-module"

From Dev

Failed to load kafka module

From Dev

play framework javascript Routing Failed to load resource

From Dev

express error - cannot find module - static file

From Dev

Failed to load fxml file

From Dev

Tornado static file routing

From Dev

Static routing in node express

From Dev

Failed to load module in Angular JS

From Dev

Load coffee script module in Angular

From Dev

django failed to load static image

From Dev

node express routing pass variable to required module

From Dev

Failed to load module script: The server responded with a non-JavaScript, CSS MIME type of "text/x-scss"

From Dev

Blazor - Static Html file routing

From Dev

Failed to load module script when importing a .jpg file in a javascript file

From Dev

Angular load child module in child routing

Related Related

  1. 1

    AngularJS routing to static file

  2. 2

    Vue-resource can't load express static file

  3. 3

    Simple redirect in Express with static file

  4. 4

    Cannot load the script with the type "module"

  5. 5

    Express routing from configuration file

  6. 6

    Haskell Failed to load interface for module

  7. 7

    express static file serve configuration

  8. 8

    DLL load failed: The specified module could not be found. File "<stdin>", line 1, in <module>

  9. 9

    Electron Failed to load Module

  10. 10

    How to fix: "Failed to load module script ..." Angular 8, Electron 5

  11. 11

    Failed to load module script because of a disallowed MIME type

  12. 12

    Can run script from pycharm, but not from Terminal. ImportError: DLL load failed: The specified module could not be found

  13. 13

    Express middleware for express.static() not routing correctly and throwing next() is not a function

  14. 14

    Express File Serving and Static Files

  15. 15

    Failed to load module "canberra-gtk-module"

  16. 16

    Failed to load kafka module

  17. 17

    play framework javascript Routing Failed to load resource

  18. 18

    express error - cannot find module - static file

  19. 19

    Failed to load fxml file

  20. 20

    Tornado static file routing

  21. 21

    Static routing in node express

  22. 22

    Failed to load module in Angular JS

  23. 23

    Load coffee script module in Angular

  24. 24

    django failed to load static image

  25. 25

    node express routing pass variable to required module

  26. 26

    Failed to load module script: The server responded with a non-JavaScript, CSS MIME type of "text/x-scss"

  27. 27

    Blazor - Static Html file routing

  28. 28

    Failed to load module script when importing a .jpg file in a javascript file

  29. 29

    Angular load child module in child routing

HotTag

Archive