I have the following code that I am trying to run as part of learning React.
HTML
<html> <head> <link href="index.css" rel="stylesheet"> </head> <body> <div id="root"> </div> <script src="index.js"></script> </body> </html> Javascript
import React from "react" import ReactDOM from "react-dom" const navbar= ( <nav> <h1>Alchamentry</h1> <ul> <li>Pricing</li> <li>About</li> <li>Contact</li> </ul> </nav> ) const root = ReactDOM.createRoot(document.getElementById("root")) root.render(navbar) package.json
{ "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0" } } Now when I open the browser, I get the following error:
index.js:1 Uncaught SyntaxError: Cannot use import statement outside a module (at index.js:1:1) and nothing is displayed on the browser. How to fix this? I tried to add the module to the script tag, but it is giving another error that says
<script src="index.js" type="module"></script> index.js:1 Uncaught SyntaxError: Cannot use import statement outside a module (at index.js:1:1)
"type": "module"inpackage.json. i think. more on thatUncaught SyntaxError: Unexpected token '<' (at index.js:5:5). this is where<navstartsUncaught SyntaxError: Cannot use import statementerror is solved.