61

I'm getting this error in an almost empty React component:

[ts] Unterminated regular expression literal.

import * as React from 'react';

export default class EmptyComponent extends React.Component {

    render() {

        return (
            <p>Hello</p>
        );
    }
}

I don't know what I'm doing wrong!

2
  • 45
    Is it tsx file?
    – MistyK
    Dec 1, 2017 at 21:54
  • 1
    Yes. That was the problem. It wasn't!
    – JulianG
    Dec 1, 2017 at 22:16

4 Answers 4

207

It turns out I was using the .ts file extension instead of .tsx

Make sure your component file extension is .tsx (if you're using Typescript) or .jsx (if you're using Javascript).

2
  • 10
    hehe, that's what I suggested : )
    – MistyK
    Dec 1, 2017 at 21:55
  • 1
    I am facing the same issue. I am using .jsx file Aug 15, 2018 at 10:31
7

So my case was a bit unique. I had the same error message. but everything got fix after I restarted the build process (e.g. in this case I was working with storybook, so npm run storybook). The symptom was that, even I changed my file name to be .tsx the error still reporting the same file as .ts. That reminded me that I changed the file name when the build is already watching and running. That's when I decided restart the build command and wolaa! everything fixed itself.

Sometime its just that --- "Have you turn it off and on again?"

1
  • yes, you should use tsx. instead of ts!!!
    – Frank Guo
    Nov 7, 2022 at 13:46
3

Just in case someone else runs across this and has named their file appropriately, re-inspect your regex to make sure you haven't accidentally created an invalid regex. For example, mine looked like:

/^https?:\/\/

and it should have been:

/^https?:\/\//
             ^ left this lil' guy off

You can also use an online regex tool to make sure you've created a valid regex.

0

I'm using WebStorm. The file is a .tsx file and turned out that after I close the file and re-open the file again, the issue is gone itself.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.