React Refers To UMD Global, But The Current File Is A Module – How To Fix It?

React refers to UMD global, but the current file is a module

The error “React refers to UMD global, but the current file is a module” related to your Typescript version. But don’t worry! It is also easy to fix. Let’s go into detail.

The reason for the error “React refers to UMD global, but the current file is a module”

Example of the error:

const Component = () => {
    return <>Hello World</>;
}

export default Component;

Error:

This error occurs when you use an earlier version of TypeScript, which is old enough to get the error. The old version required you to type a React statement every time it contained jsx to make your react recognize your file as a component.

Solutions for this error

Use import statement

Of course, the easiest solution is to use import React, which ReactJS recommends.

import React from "react";

const Component = () => {
    return <>Hello World</>;
}

export default Component;

Here I import React, so React will record this file as a component, and I won’t get any errors anymore.

Update your device

Make sure to update all these things. You won’t get any errors like this happening anymore.

  • Typescript version 4.1 or later
  • React and react-dom version 17 or later

These newer version will make importing React become optional.

You can update it by this command below.

To install the latest version of TypeScript:

npm install –g typescript@latest version

Or you can run:

npm update

This command will help you to update all packages, including TypeScript.

If you want to install a particular version of Typescript, you can run:

npm install –g typescript@typescript-version

Example:

npm install –global [email protected]

To update React and React-dom:

npm install react@latest react-dom@latest

Set the tsconfig.json file

Make sure your tsconfig.json file has this line:

"jsx": "react-jsx"

Or can it be

"jsx": "react-jsxdev"

Example:

{
	...
    "jsx": "react-jsx"
    ...
}

It relates to React JSX Factories. You can read more about React 17 JSX factories here.

Summary

In this tutorial, I have explained how to solve the error “React refers to UMD global, but the current file is a module”. You can update to the latest Typescript and React version, and that error will never bother you. If you have any questions or problems, please comment below. I will answer as possible. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *