User_Targaryen
User_Targaryen

Reputation: 4225

Uncaught ReferenceError: require is not defined React.js

I am writing the following in my React code:

var React = require('react');

I followed the React setup from tutorialspoint. I installed all the things at /Desktop/reactApp/. The React code is being run from /GitProjects/DjangoProjects/MyProj/MyApp/static/react/dashboard.js. I keep on getting the error Uncaught ReferenceError. Am I missing something?

Note: HTML file at /GitProjects/DjangoProjects/MyProj/MyApp/templates/dashboard.html is calling my dashboard.js code.

Upvotes: 0

Views: 3694

Answers (1)

Kos
Kos

Reputation: 72221

It sounds like you're trying to include a file dashboard.js in HTML.

If this file contains code like require('react'); then it means that you need to first compile it using a build tool that will actually find react and bundle it together with your own code.

In case of the tutorial you're linking, the build tool used is webpack. Your HTML should include the file generated by webpack (index.js, or whatever you have in output section of webpack config), rather than dashboard.js alone.

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
   },

Upvotes: 1

Related Questions