Error showing when I run this react code on VScode. Can anyone please solve?

Viewed 25
import React from "react"
import ReactDOM from "react-dom"

const page = (
  <div>
    <img src="./react-logo.png" width="40px"/>
    <h1>Fun Facts</h1>
    <ul>
      <li>was 1st released in 2013</li>
      <li>created by jordan walke</li>
      <li>100k stars on github</li>
      <li>maintained by fb</li>
      <li>powers 100ks of apps</li>
    </ul>

  </div>
)

ReactDOM.render(page, document.getElementById("root"))

import React from "react" ^^^^^^

SyntaxError: Cannot use import statement outside a module

1 Answers

In the nearest parent package.json file, add the top-level "type" field with a value of "module". This will ensure that all .js and .mjs files are interpreted as ES modules.

// package.json
{
  "type": "module"
}
Related