简体   繁体   中英

while using npm start in the terminal of visual studio I get an error

I am using JavaScipt ES6 with Node. I am using visual studio code. I am getting this error when I run npm start: The error is:

×  C:\Users\markp\source\repos\bioinvisionTest\index.html:1:31: Imports and requires are not supported inside inline <script> tags yet

in other programs the import works. This one it doesn't. All that is in the program is this:

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="demo"></div>    
  <script>
    import TestComponent from "./components/Testcomponent"
  </script>     
</body>
</html>

and in the components folder there is file called Testcomponent js that contains this:

function TestComponent() {
  console.log("test component js")
}

export default {
  TestComponent
}

return key is somting which you can't simply ignore if it's multiline codes.

If you are uinsg Creaet-react-app as generator then i will recommend use APP.js as base and inject your component in that file for now. Later you can redesign.

If you are using only HTML and JS file then you should import react and react-dom library in your index.html file.

One page React App, place in your html file.

<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<script type="text/babel">
class Greeting extends React.Component {
    render() {
        return (<p>Hello world</p>);
    }
}
ReactDOM.render(
    <Greeting />,
    document.getElementById('root')
);
</script>

Modules are natively suppported refer MDN docs

Try adding type="module" to your script tag.

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="demo"></div>

  <script type="module">
  import TestComponent from "./components/Testcomponent"




  </script> 



</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM