Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require is not defined

Im building a new React app but get the following error - "require is not defined"

hello-world.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="react/react.js"></script>
    <script src="react/react-dom.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script> 
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel" src="hello-world.js">
  </body>
</html>

hello-world.js

import React from 'react';
import ReactDOM from 'react-dom';

import App from './App.jsx';

ReactDOM.render(
        <App />,
        document.getElementById('example')
      );

App.jsx

import React from 'react';

class App extends React.Component {
   render() {
      return (
         <div>
            Hello World!!!
         </div>
      );
   }
}

export default App;

Im running this from my client and don't have any web server running.

I tried to include http://requirejs.org/docs/release/2.2.0/minified/require.js but it gives a totally different error.

like image 203
user1050619 Avatar asked Apr 18 '16 15:04

user1050619


People also ask

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

Why is require not defined JavaScript?

The JavaScript require() function is only available by default in Node. js environment. This means the browser won't know what you mean with the require() call in your code. But require() is actually not needed because browsers automatically load all the <script> files you added to your HTML file.

How do you fix require is not defined in NodeJS?

It happens when you declare your package type as module in your package. json . If you do this, certain CommonJS variables can't be used, including require . To fix this, remove "type": "module" from your package.

Can't use require in HTML?

The reason you are getting ReferenceError: require is not defined is because nowhere in your html page is Require included. Require does not come with your standard JavaScript library. You must include the file on your page so it can be loaded and used. This can be done by simply adding <script src="myJS.


3 Answers

You're trying to use a CommonJS module from within your browser. This will not work.

How are you using them? When you write import ... from ... in ES6 Babel will transpile these calls to a module definition called CommonJS and since CommonJS isn't around in the browser you'll get an undefined error from require().

Furthermore, you're also trying to load RequireJS which uses a different module definition pattern called AMD, Asynchronous Module Definition, and will not take care of the require calls for you. You can wrap them in RequireJS specific calls.

If you want to use CommonJS modules in your code base you need to first bundle them with either Browserify or webpack. The two tools will transform your require calls to some glue magic that you can use within the browser.

But in your specific case, if you remove the import calls and just let the browser take care of and attach the classes you've created to the window object your code should work.

like image 94
Henrik Andersson Avatar answered Sep 17 '22 13:09

Henrik Andersson


Also, note that when you are using submodules from React without another transpiler you will have to reference the top-level modules. I just know that in some cases there are people who will not wish refactor their projects and alter directories to handle webpack/browserify module imports.

So instead of using the CommonJS import React, {useState, useEffect} from 'react' you can instead reference hooks with React.useEffect() , React.useState().

Just another solution for those who don't want to deal with any refactoring.

like image 21
Brian Anderson Avatar answered Sep 17 '22 13:09

Brian Anderson


Here is a basic hello world example on hooks The code samples on react website doesn't show the full html document. In summary use React.useEffect and React.useState. Get rid of the import statement.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World</title>
    <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">

function Example() {
  const [count, setCount] = React.useState(0);

  // Similar to componentDidMount and componentDidUpdate:  
  React.useEffect(() => {    // Update the document title using the browser API    
    document.title = `You clicked ${count} times`;  });
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

ReactDOM.render(
        <Example />,
        document.getElementById('root')
      );
    </script>
  </body>
</html>
like image 27
Omar Khan Avatar answered Sep 21 '22 13:09

Omar Khan