0

I am adding react to my existing html page. by following the instructions here enter link description here. And then this works just fine, until I import a different component.

my html codes:

    <!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>Test2</title>
</head>
<body>
   <h1>Hello World</h1>
   <div id="like_button_container"></div>

   <!-- Load React. -->
   <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
   <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin>. 
   </script>
   <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" 
    crossorigin></script>

  <!-- Load our React component. -->
  <script src="like_button.js"></script>

</body>
</html>

my like_button.js codes:

    'use strict';
import Another from "./another.js"; //problem here, if you comment this line, it works.

const e = React.createElement;

class LikeButton extends React.Component {
     constructor(props) {
     super(props);
     this.state = { liked: false };
   }

  render() {
        if (this.state.liked) {
       return 'You liked this.';
   }

  return e(
     'button',
     { onClick: () => this.setState({ liked: true }) },
     'Like'
     );
   }
 }

const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);

and my another.js codes:

   'use strict';

  const e = React.createElement;

 class Another extends React.Component {
       constructor(props) {
       super(props);
       this.state = { liked: false };
    }

  render() {
       if (this.state.liked) {
       return 'You liked this (another).';
    }

   return e(
      'button',
      { onClick: () => this.setState({ liked: true }) },
     'Like'
    );
   }
  }

  export default Another;

thank you for your answer.

edit: i added "type": "module" in package.json file. The latest situation is as follows;

  {
   "name": "test2",
   "version": "1.0.0",
   "description": "",
   "main": "another.js",
   "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
     },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "type": "module"
  }

but it still didn't work, I get the same error in the console.

edit2: fix the problem. Just adding ("type": "module") to package.json was not enough, I also had to add (type="module") to the script tag in the html file.

1 Answer 1

2

You need to tell node that you want to use ESmodule features such as the import and export keywords.

To do this add this line to your package.json file.

"type": "module"

An example package.json file that specifies type.

{
    "name": "pup",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "type": "module"
}

You also need to add type="module" to the script tag where you import the file in index.html.

<script src="like_button.js" type="module"></script>

This will stop the error that you are getting.

Also worth looking at commonjs vs es6 imports

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

7 Comments

thank you your answer, i try this but I am still getting the same error. first in terminal "npm init -y" and i create package.json file. then add ""type": "module" . doesn't work. If you want, you can copy my codes and try.
By default "type": "module" isn't added when you use npm init -y, did you add it manually? @malibil
yes i added manual, The latest situation is as follows; { "name": "test2", "version": "1.0.0", "description": "", "main": "another.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "type": "module" }
@malibil I've made a new folder with all the files set up like you said and I can't reproduce the error. Is there anything that you left off your post?
@malibil My bad sorry, I spent 5 minutes staring at your javascript and completely forgot about html. I think I've fixed the problem now, and made an edit to the post.
|

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.