1

I m using node.js for this and using react as frontend, Now how do I import app.jsx file to index.js file? index.js

import App from "./components/app";

ReactDOM.render(<App text="Hello world"/> , document.getElementById('root'))

app.jsx


function App(props){
    return <h1>{props.text}</h1>
}

export default App;

now at last in app.jsx file I tried to import/require the React file but I get an error "require is not defined"

also in console when importing app.jsx it says require is not defined i m using import and that's what it says

2
  • try adding the filetype in the import statement import App from "./components/App.jsx"; Commented Dec 23, 2021 at 13:21
  • Does this answer your question? importing React component file with node js Commented Dec 23, 2021 at 15:16

3 Answers 3

1

if there is "type":"module" in package.json, remove it.

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

4 Comments

there is no "type" module
I guess it might be a problem in node. Does the error occur other project file? or just in this project?
This is my 1st project with node react
If i create a react app, i cant create different routes, cant get data from mongoose
0

use npx create-react-app in your terminal to create new starter react app, you will see the working code over there. link for more info https://create-react-app.dev/docs/getting-started

1 Comment

i m not talking about react app, i want a backend with reactjs as frontend
0

Did you type code below at the top in index.js and app.jsx file?

// app.jsx
import React from "react";

// index.js
import React from "react";
import ReactDom from "react-dom";

or maybe you type "require" code in the wrong line. Check it!

2 Comments

whenever i type import a error comes saying that require Is not defined, but this code works for me ReactDOM.render(<h1>Hello World</h1> , document.getElementById('root'))
my problem is i cant import anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.