I've been struggling with this problem for days. The problem is that import works badly and I don't know why. Here are some examples:
I have an index.html, completely empty, I only have the script. Like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module" src="/src/index.js"></script>
</body>
</html>
Inside src folder I have index.js and app.js. In app.js the code is:
const helloWorld = () => {
console.log('Hello world');
};
export default helloWorld;
In index.js the code is:
import helloWorld from 'app.js';
helloWorld()
I use the VSC Live Server extension to mount the server, the Chrome console said:
Uncaught TypeError: Failed to resolve module specifier "app.js". Relative references must start with either "/", "./", or "../".
I said: "Ok", and I try this in index.js:
import helloWorld from './app';
helloWorld()
Chrome console said:
GET http://127.0.0.1:5500/src/app net::ERR_ABORTED 404 (Not Found)
This problem also happens to me with all npm packages that are in node_modules. Examples:
import bootstrap from 'bootstrap';
import axios from 'axios';
Chrome console:
Uncaught TypeError: Failed to resolve module specifier "bootstrap". Relative references must start with either "/", "./", or "../".
Uncaught TypeError: Failed to resolve module specifier "axios". Relative references must start with either "/", "./", or "../".
VSC recognizes the axios and the bootstrap routes but when trying in the browser something breaks. Examples:
I also tried using another local server, but it didn't work either. I tried the one explained in this link:
How to open browser to localhost through npm scripts
Something else to add is that it says the error is on line 1 of the index.html: