I am having trouble while trying to import a component with React.
app.js:1 Uncaught ReferenceError: require is not defined at :5:12 at i (babel.min.js:24) at r (babel.min.js:24) at e.src.n.(:3000/anonymous function).l.content (https://unpkg.com/[email protected]/babel.min.js:24:30503) at XMLHttpRequest.n.onreadystatechange (babel.min.js:24)
After some research I found that the error was caused because the browser doesn't understand import so I tried to install webpack, It didn't work, so I made some other research, finally I had to disable the windows firewall, then I was able to run the npm install, and I got this :
- [email protected] added 157 packages from 127 contributors and audited 36574 packages in 18.717s found 31 high severity vulnerabilities run
npm audit fixto fix them, ornpm auditfor details
But nothing appears into my project.
There is all my project down below :
index.html
<!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, shrink-to-fit=no"/> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="app"></div> <script src="https://unpkg.com/[email protected]/babel.min.js"></script> <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <script src="./src/components/app.js" type="text/babel"></script> </body> </html> /src/components/com.js
class Com extends React.Component { render() { return <div> <h1>Hey this is my com component</h1> </div> } } export default Com /src/components/app.js
import Com from './com.js' class App extends React.Component { render() { return <div> <h1>Hey this is my app component</h1> <p>{Math.random() * 10}</p> <Com /> </div> } } ReactDOM.render(<App />, document.getElementById('app')) Hope someone can help me figure it out.
(I would like to say Hello or Hi at the beginning of my question but I don't know why I can't edit it, so Hello here.)
EDIT
I finally have webpack working.
webpack.config.js
const webpack = require("webpack") const path = require("path") let config = { entry: "./src/index.js", output: { path: path.resolve(__dirname, "./public"), filename: "./bundle.js" }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader", options: { presets: [ '@babel/preset-env', '@babel/preset-react' ] } } ] } } module.exports = config I don't know if it is correct, but I do have Babel working, and I can import component correctly.