Skip to content Skip to sidebar Skip to footer

Module Type Is Not Detectable On A Nodejs With Vanillajs Configured Project

I created for this project, a nodejs server configured to accept javascript as es6 modules. Server runs successfully, however when I am reaching the url: localhost:3333, am getting

Solution 1:

I created for this project, a nodejs server configured to accept javascript as es6 modules.

That doesn't matter.

Your error message is client side.

You have two different JS programs. One running through Node.js (which creates a web server) and one running in the browser.

The error message is from the latter.


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

So let's trace this:

<scripttype="module"src="index.js"></script>

The browser is asking for http://localhost:3333/index.js and getting an HTML document. That isn't a JS file, so it errors.

Likely you don't have an index.js file in the express folder where you told Express to look for static files.

Solution 2:

Solution 3:

Best way to configure your NodeJS to accepting E6 Syntax is by using Babel and not the type:module thing(Personal believe).

To get started install the following packages

  • npm i @babel/core @babel/node @babel/preset-env
  • If your using yarn then yarn add @babel/core @babel/node @babel/preset-env

Now create a file called:.babelrc

  • In the .babelrc file do the following below
{
    "presets":["@babel/preset-env"]
}

Finally time to setup your package.json scripts

  • "start":"babel-node server.js"
  • "dev":"nodemon --exec babel-node server.js"

By doing this now your NodeJS will be using ES6 and you wont even face any problems

Post a Comment for "Module Type Is Not Detectable On A Nodejs With Vanillajs Configured Project"