未捕获的ReferenceError: require在React.js中没有定义

Uncaught ReferenceError: require is not defined React.js

本文关键字:js React 定义 require ReferenceError      更新时间:2023-09-26

我在我的React代码中编写以下内容:

var React = require('react');

我遵循教程点的React设置。我安装了所有的东西在/Desktop/reactApp/。React代码是从/GitProjects/DjangoProjects/MyProj/MyApp/static/react/dashboard.js运行的。我一直得到错误Uncaught ReferenceError。我错过什么了吗?

注意:HTML文件在/GitProjects/DjangoProjects/MyProj/MyApp/templates/dashboard.html是调用我的dashboard.js代码。

听起来你想在HTML中包含一个文件dashboard.js

如果这个文件包含像require('react');这样的代码,那么这意味着你需要首先使用构建工具编译它,该工具将实际找到react并将其与你自己的代码捆绑在一起。

如果你链接的教程,使用的构建工具是webpack。你的HTML应该包括webpack生成的文件(index.js,或者webpack配置中output部分的任何文件),而不是单独的dashboard.js

webpack.config.js

var config = {
   entry: './main.js',  # the main code of your app that require()s other files
   output: {
      path:'./',
      filename: 'index.js',  # the file you should be including in HTML
   },