1

I am trying to build the simplest react component. It just renders a Hello page, but i am getting an Error in my console. I am using typescript as my compiler. I am trying to follow this guide to get the setup for my project: https://github.com/Microsoft/TypeScript-React-Starter

Error

Uncaught ReferenceError: React is not defined
    at Object.<anonymous> (external "React":1)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at Object.module.exports (index.tsx:1)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at Object.<anonymous> (bundle.js:3669)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at module.exports.ctor.super_ (bootstrap 2fd03459d628585593a4:62)
    at bootstrap 2fd03459d628585593a4:62

Here is my WebPack config:

module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js",
        path: __dirname + "/dist",
        publicPath: "/dist/"
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    devServer: {
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        port: 8282
    },

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

Here is myindex.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";

import { Hello } from "./components/Hello";

ReactDOM.render(
    <Hello compiler="TypeScript" framework="React" />,
    document.getElementById("example")
);

And here is my hello component

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }

// 'HelloProps' describes the shape of props.
// State is never set so we use the 'undefined' type.
export class Hello extends React.Component<HelloProps, undefined> {
    componentWillMount () {
        if ('pluginLoaded' in window) {
            (window as any).pluginLoaded('hello', function (port: any, context: any) {
                // Future work should interact with the message channel here
            });
        }
    }

    render() {
        return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
    }
}

1 Answer 1

6

You defined react and react-dom as Externals. As you've written in the comment for the external option in your webpack config, it is expected that the corresponding global variable exists.

You have to include a version of React in your HTML. React can be used from a CDN by including the following scripts in your HTML.

<script crossorigin src="https://unpkg.com/react@15/dist/react.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>

This is the development version of React and if you intend to use it in production you should use the minified versions. For more details see React - Using a CDN.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup. You Are right. I just commented out the external config in Webpack. If I did not comment it out I would have had to include them with script tags as you mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.