0

I'm getting this error though I don't know what I'm doing wrong (main.mjs and connect-database.mjs work when they aren't connected to html file)

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

index.html:

enter image description here

main.mjs:

enter image description here

connect-database.mjs:

enter image description here

Jojo Momo
  • 21
  • 1
  • 9
  • The server sending the `.mjs` file to the html page is not configured to send the correct [`content-type`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) header for `.mjs` files. Until fixed, does it work if the HTML loads a copy of the `.mjs` file renamed to have a `.js` file extesion? – traktor Sep 17 '22 at 23:16
  • What kind of server are you using for the html page? – traktor Sep 17 '22 at 23:37
  • @traktor So what should I write for content-type and where? – Jojo Momo Sep 18 '22 at 00:01
  • @traktor I changed main.mjs to main.js and now I have this error: Uncaught TypeError: Failed to resolve module specifier "cassandra-driver". Relative references must start with either "/", "./", or "../". – Jojo Momo Sep 18 '22 at 00:03
  • It looks like you are trying to run an es module script ("main.mjs") written to run in a node server, which is trying to import the [cassandra server](https://www.npmjs.com/package/cassandra-driver) package from a [`node_modules`](https://stackoverflow.com/q/63294260/5217142) folder. Node scripts are not designed to run in a browser. – traktor Sep 18 '22 at 09:26

1 Answers1

0

Node scripts use the node run-time environment and have program access to resources on the server. They are not compatible with browser run time environments which have a different security model and architecture. Try sending requests to perform, or involve performing, database updates to endpoints on the server which update the DB as part of their request handling.

Typical methods of sending requests from the browser include the use of

  • form submission
  • extracting data from a form and posting it to the server as a FormData object, possibly using XMLHttpRequest to post it,
  • the fetch API,
  • HTTP request abstractions provided by front-end libraries included in the web page such as axios, or jQuery.ajax
traktor
  • 17,588
  • 4
  • 32
  • 53