1

I am learning React and I have written this code:

import React from "react";
import ReactDOM from "react-dom";
const element = <h1>Hello World!</h1>;
ReactDOM.render(element, document.getElementById('root');

However, on executing the Programme, I am getting this error:

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47

As a can't understand what is the problem in the code. Please do help me out.

5
  • where do you put those imports? Commented Feb 28, 2021 at 5:11
  • Can you provide more information? It seems like you are running it in Node but it's a little hard to tell. Commented Feb 28, 2021 at 5:12
  • I put the import in App.js Commented Feb 28, 2021 at 5:13
  • 1
    Looks like your transpiler might not be configured right? Commented Feb 28, 2021 at 5:19
  • How can I configure that? Commented Mar 2, 2021 at 10:50

4 Answers 4

1

From what I can see you should correct the typo in the word document, close your render function and give it the element to render:

ReactDOM.render(element, document.getElementById('root'));
Sign up to request clarification or add additional context in comments.

1 Comment

I would also add that it is worth starting by setting up your code editor to see such typos.
0

You have a missing element here, so you must need to pass your created element const variable to render() method as your first argument to render your created element to your DOM

So try and review this code.

const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));

Use this reference to know more about React Rendering Elements

1 Comment

On using this code I am getting the same error: (node:22004) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use node --trace-warnings ... to show where the warning was created) c:\Users\Agniva Roy\master\src\App.js:1 import React from "react"; ^^^^^^ SyntaxError: Cannot use import statement outside a module
0

From the React docs, ReactDOM.render Render a React element into the DOM in the supplied container, below is the syntax

ReactDOM.render(element, container[, callback])

So the two problems in your code are, document spelling and the ReactDOM.render arguments

import React from "react";
import ReactDOM from "react-dom";
const element = <h1>Hello World!</h1>;
ReactDOM.render(element , document.getElementById('root'));

Below is the runnable snippet

const element = <h1>Hello World!</h1>;
ReactDOM.render(element , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Comments

0

You need to pass your element to render() method as your first argument to render that element and also there is a typo in the word document and also your render function is not closed. Try these two things first. For your help =>

const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));

Comments

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.