React-Bootstrap Uncaught ReferenceError: require is not defined

孤街醉人 提交于 2019-12-11 16:59:36

问题


I am new to react, I am trying to use react-bootstrap in a project. However I could not get it worked, the example on react-bootstrap page at https://react-bootstrap.github.io/getting-started.html. I only included below code in my html, but I am getting react.production.min.js:10 Uncaught ReferenceError: require is not defined error. Actually I have read other questions and many tutorials, still could not even include correct files from cdn. What should be the correct files or do I need to any other scripts to get it worked. Thank you,,

<!-- Bootsrap and react js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/cjs/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/0.31.5/react-bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/cjs/react-dom.production.min.js"></script>

Full error trace is below :

Uncaught ReferenceError: require is not defined
at react.production.min.js:10
 react-bootstrap.min.js:10 Uncaught TypeError: Cannot read property    'Component' of undefined
at Object.e.__esModule.default (react-bootstrap.min.js:10)
at t (react-bootstrap.min.js:1)
at Object.e.__esModule.default (react-bootstrap.min.js:11)
at t (react-bootstrap.min.js:1)
at Object.t.exports (react-bootstrap.min.js:1)
at t (react-bootstrap.min.js:1)
at t (react-bootstrap.min.js:1)
at react-bootstrap.min.js:1
at react-bootstrap.min.js:1
at react-bootstrap.min.js:1

react-dom.production.min.js:13 Uncaught ReferenceError: require is not defined at react-dom.production.min.js:13 1:86 Uncaught ReferenceError: ReactBootstrap is not defined at 1:86


回答1:


If you are using it in a browser, don't follow the CommonJS section until you are using webpack or something similar. Browsers are asynchronous, therefore you cannot use require synchronously. Follow the ES6 (if you are transpiling with Babel) or AMD (with RequireJS or similar) instructions instead.

If you don't know what any of those mean, follow the "Browser globals" instructions to include everything:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/<react-version>/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/<react-version>/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/<version>/react-bootstrap.min.js"></script>
<script>
var Alert = ReactBootstrap.Alert;
</script>


来源:https://stackoverflow.com/questions/47546379/react-bootstrap-uncaught-referenceerror-require-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!