![](/img/trans.png)
[英]Uncaught ReferenceError: require is not defined at app.js:3
[英]“Uncaught ReferenceError: require is not defined” when importing into App.js for React App created using CDN links
我創建了一個 React 應用程序,它向瀏覽器呈現帶有“Hello World”的 h1 標記。 我使用 CDN 鏈接而不是 npm 設置了應用程序。 該應用程序包含三個文件:
現在的挑戰是當我在我的 App.js 頂部添加import './styles.css'
時,應用程序中斷並且瀏覽器上沒有顯示任何內容。 檢查瀏覽器控制台后,我發現錯誤消息:
“App.js:1 Uncaught ReferenceError: require is not defined”-這是來自瀏覽器控制台的錯誤消息截圖
我的問題是如何將腳本/其他反應組件導入我的 App.js 而不會收到此錯誤消息? 為什么 babel 給我這個錯誤?
下面是index.html、style.css和App.js代碼:
class App extends React.Component{ render(){ return( <div> <h1 className="heading">Hello World</h1> </div>) } } ReactDOM.render(<App/>, document.getElementById('root'))
.heading{ display: flex; text-align: center; font-size: 30px; color: blueviolet; }
<:DOCTYPE html> <html> <head> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"> <link rel="stylesheet" src="style:css"> <script src='https.//unpkg.com/react@16.3.1/umd/react.production.min:js'></script> <script src='https.//unpkg.com/react-dom@16.3.1/umd/react-dom.production.min:js'></script> <script src='https.//unpkg.com/babel-standalone@6.26.0/babel.js'></script> </head> <body> <div id="root"></div> <script src="App.js" type="text/babel"> </script> </body> </html>
我終於發現最簡單的解決方案是像這樣鏈接到我的 index.html :
<link rel="stylesheet" href="styles.css">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.