Reactjs: Uncaught Referenceerror: Require Is Not Defined

ReactJS: Uncaught ReferenceError: require is not defined

If you are not using any module bundler like webpack or etc.
You should assign you components to some javascript global object, because objects from .jsx are not put in global scope

So here is the solution (used window object here)

Defined module:

window.AdminMenu = React.createClass({
getInitialState: function() {
return {};
},
render: function() {
return (
<h1>Menu</h1>
);
}
});

Where you use it:

ReactDOM.render(
<window.AdminMenu/>,
document.getElementById('content')
);

React - Uncaught ReferenceError: require is not defined

You need to use something like Rollup, Webpack, or Browserify. This statement import FormComponent from './FormComponent.js'; doesn't mean anything on the client. No browser natively supports it so you need something like the tools mentioned above to turn it into something the browser can actually use.

Without them you just have to load the files in your index.html.

React, Getting error: Uncaught ReferenceError: require is not defined

Don't know if I fully understand the question but you seem to be using React.js from a cdn link. With no module bundler. So why are you importing it also? You've already "imported" React.js from your cdn link. =) No need to import it again. The import statement is when you use modules and import them like that with the ES6 syntax. If you want to do that use create-react-app instead.

ReferenceError: require is not defined @react

The externals configuration seems wrong – you probably only need externals: [nodeExternals()] (from const nodeExternals = require("webpack-node-externals");).

reactjs error Uncaught ReferenceError: require is not defined

Please try this,

Main html file where you are using cdn add this.

<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel"></script>

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

<!-- ... existing HTML ... -->

<div id="new_component_container"></div>

<!-- ... existing HTML ... -->

In the component file do this,

import ReactBsTable from  'react-bootstrap-table';
var BootstrapTable = ReactBsTable.BootstrapTable;
var TableHeaderColumn = ReactBsTable.TableHeaderColumn;

var products = [{
id: 1,
name: "Product1",
price: 120
}, {
id: 2,
name: "Product2",
price: 80
}];

const domContainer = document.querySelector('#new_component_container');
ReactDOM.render(
<BootstrapTable data={products} version='4'>
<TableHeaderColumn isKey dataField='id'>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>,
domContainer);

For react bootstrap table also , use import only if you are installing the library. Again if you are using cdn for that too then import won't work.

For more info related to react cdn please view this.

https://reactjs.org/docs/add-react-to-a-website.html



Related Topics



Leave a reply



Submit