search

Home  >  Q&A  >  body text

Rewrite the title to: Translate "SyntaxError: Cannot use import statement outside a module" into Chinese as "SyntaxError: Cannot use import statement outside a module" when running on the live server

<p>//This is the greet.js file</p> <pre class="brush:php;toolbar:false;">import React from "react"; functionGreet() { return ( <div><h1>Welcome</h1></div> ) } export default Greet</pre> <p>//This is the indx.js file</p> <pre class="brush:php;toolbar:false;">import React,{Component} from 'react' import Greet from './Greet' class App extends Component { render() { <div className='App'> <Greet/> </div> } }</pre> <p>//This is an html file</p> <pre class="brush:php;toolbar:false;"><!DOCTYPE html> <html> <head> <title> Here We Go </title> </head> <body> <script src="indx.js"> </script> </body> </html></pre> <p>I'm trying to get data from greet.js and render it in indx.js, but I get the <strong>Cannot use import statement outside module error</strong></p>
P粉520545753P粉520545753456 days ago652

reply all(1)I'll reply

  • P粉885035114

    P粉8850351142023-08-29 16:24:30

    • The reason you are getting the error is because you need to add type='module' to the script tag to use the import/export syntax
    • You cannot use JSX in the browser. You can use babel, esbuild, or your favorite compiler on the server, or type babel directly into HTML. You can also use vite

    reply
    0
  • Cancelreply