2

I have 3 files one HTML, 2 javascript files named app.js and customer .js

I have an HTML page on which I have written this

<!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>Document</title>
</head>
<body>
    hello
    <script src="app.js" type="module"></script>
</body>
</html>   

I just loaded app.js into this page

app.js contains this

import {person} from "./customer";
console.log("helllo");

and in customer.js i have this

const person={
    name:"hello"
}

export default person;

I am getting an import error the error shows

GET http://127.0.0.1:5500/customer net::ERR_ABORTED 404 (Not Found)

I am new to web development, please help me.

7
  • What do you use for routing? Commented Sep 16, 2021 at 17:33
  • 1
    Remove the "." from your import statement, and add the extension. Try import {person} from "/customer.js"; You're relying on the browser, not a Javascript bundler. Commented Sep 16, 2021 at 17:34
  • Please share your directory structure and what server you are using. Commented Sep 16, 2021 at 17:34
  • @TJBlackman I just changed in that way and this new error pops up Uncaught SyntaxError: The requested module '/customer.js' does not provide an export named 'person' Commented Sep 16, 2021 at 17:37
  • 1
    Look at the difference in export and export default. Remove the curly braces if you want to keep using export default. import person from "/customer.js Commented Sep 16, 2021 at 17:39

2 Answers 2

1

Here, the way you are importing it is wrong.

Let me explain. First of all, I can see that you are default exporting the module or whatever. In that case, you don't need those curly brackets.

import person from "./customer.js";

Second, just make sure that in your package.json you have "type" set to "module" just like this

"type": "module",

Third, if you are using VSCode, then you can right-click on your customer.js file and click on copy relative path to get its relative path from your project.

Summary:

  1. Correct your import statement
  2. Check if package.json is correct
  3. Copy relative path using VSCode

Hope this helps to solve your query!

Sign up to request clarification or add additional context in comments.

Comments

-1

if you got a:

1) index.html:1 Access to script at 'file://$PATH'  from origin 'null' has been blocked by CORS policy:  Cross origin requests are only supported for protocol schemes:  http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

2) GET file://$PATH net::ERR_FAILED

You need to use local-web-server, for example Live Server in VS Code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.