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

Codie

I'm just trying to import an image in a javascript file to test webpack assets. When I start the Live Server extenssion in VS-Code, I should see the image but I receive following Error:

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

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello world!</title>
  </head>
  <body>
    <script type="module" src="./src/index.js"></script>
  </body>
</html>

index.js

import tech2 from "./tech2.jpg";

function addImage() {
  const img = document.createElement("img");
  img.alt = "Tech.";
  img.width = 300;
  img.src = tech2;
  const body = document.querySelector("body");
  body.appendChild(img);
}

addImage();

webpack.config.js:

module.exports = {
  entry: "./src/index.js",
  output: {
    path: __dirname + "/dist",
    filename: "bundle.js",
  },
  mode: "none",
  module: {
    rules: [
      {
        test: /\.(jpg|png)$/,
        type: "asset/resource",
      },
    ],
  },
};

There is nothing in the body!:

There is nothing in the body.

Quentin

Webpack has features that can take an import pointing to an image file and convert it to a URL. Browsers can't do that, the only thing you can use import to load in a browser is a JavaScript module.

You have a Webpack config, but you aren't using it.

  • You said you were using VS Code Live Server
  • The url in the browser points to src not dist (so you aren't running the webpack build pipeline manually and using the results).

If you want to use Webpack while you develop, use the Webpack dev server instead of the VS Code Live Server extension.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Express routing static file: Failed to load module script

From Dev

Failed to load file while importing modules in python

From Dev

Importing module in JS script inside HTML file

From Python

(Solved) Docstring not showing up when importing .py file as a module?

From

Flow “Required module not found” when importing a CSS file

From Dev

"Cannot find module" when importing .csv file in Create React App

From Dev

TypeScript is compiling into this failing JS file when importing a module. Why?

From Dev

Error "Cannot find module" when importing a remote JSON file in TypeScript

From Dev

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

From Dev

importing module with same name as file

From Dev

Importing a JS module in a Typescript file

From Dev

Opening a JPG file by a script in crontab

From Dev

ImportError: DLL load failed while importing numpy_ops: The specified module could not be found. when importing spacy in Python

From Dev

How to load a Javascript file without Script tag?

From Dev

Failed to load fxml file

From Dev

"ImportError: DLL load failed" when importing tensorflow

From Dev

Importing SCSS variables file into CSS module file

From Dev

Importing text file in DXL script?

From Dev

Importing the username of owner when importing file in SAS

From Dev

DLL load failed: The specified module could not be found while importing scipy

From Dev

Load Script into TypeScript File?

From Dev

SAS gz a file when importing

From Dev

Error when importing raster file

From Dev

Omit file name when importing?

From Dev

Failed to import customed python module file when using docker

From Dev

Failed to load large JavaScript file ExpressJS after uglifying

From Dev

Importing a simple Javascript file in Typescript

From Dev

Python: "ImportError: DLL load failed: The specified module could not be found." Problems when importing ffn (finance library for python)

From Dev

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

Related Related

  1. 1

    Express routing static file: Failed to load module script

  2. 2

    Failed to load file while importing modules in python

  3. 3

    Importing module in JS script inside HTML file

  4. 4

    (Solved) Docstring not showing up when importing .py file as a module?

  5. 5

    Flow “Required module not found” when importing a CSS file

  6. 6

    "Cannot find module" when importing .csv file in Create React App

  7. 7

    TypeScript is compiling into this failing JS file when importing a module. Why?

  8. 8

    Error "Cannot find module" when importing a remote JSON file in TypeScript

  9. 9

    Copying .jpg file using shell script gives 'Failed to open input stream for file' error

  10. 10

    importing module with same name as file

  11. 11

    Importing a JS module in a Typescript file

  12. 12

    Opening a JPG file by a script in crontab

  13. 13

    ImportError: DLL load failed while importing numpy_ops: The specified module could not be found. when importing spacy in Python

  14. 14

    How to load a Javascript file without Script tag?

  15. 15

    Failed to load fxml file

  16. 16

    "ImportError: DLL load failed" when importing tensorflow

  17. 17

    Importing SCSS variables file into CSS module file

  18. 18

    Importing text file in DXL script?

  19. 19

    Importing the username of owner when importing file in SAS

  20. 20

    DLL load failed: The specified module could not be found while importing scipy

  21. 21

    Load Script into TypeScript File?

  22. 22

    SAS gz a file when importing

  23. 23

    Error when importing raster file

  24. 24

    Omit file name when importing?

  25. 25

    Failed to import customed python module file when using docker

  26. 26

    Failed to load large JavaScript file ExpressJS after uglifying

  27. 27

    Importing a simple Javascript file in Typescript

  28. 28

    Python: "ImportError: DLL load failed: The specified module could not be found." Problems when importing ffn (finance library for python)

  29. 29

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

HotTag

Archive