3

when I run npm test in my project folder, I get an error message saying unexpected token import. As I am using create-react-app, I assume I don't have to configure anything because everything is supposed to work out-of-the-box.

I dont understand if I am making a mistake somewhere or its a create-react-app issue.

3 Answers 3

2

It's not an issue with create-react-app.

I had my code in src/node_modules folder which was not being transformed by babel as it excludes the node_modules.

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

1 Comment

I had the same issue / solution. My node_modules was including a dependency that was written in ES6, but wasn't compiled. I had assumed by babel install would process it, but the create-react-app doesn't run babel/webpack on node_modules.
1

For anyone finding this question from Google:

The error unexpected token import is often a sign that something is wrong with parsing your ES6 code, since import isn't a feature in earlier version. Often, browsers such as Chrome will run ES6, but you'll encounter the error when you go to run tests. Hence, running yarn test with create-react-app will expose this error.

The immediate solution, is to look at how babel is compiling your ES6 code. In the example above, create-react-app has come with webpack and babel included. However, they assume that your node_modules are already compiled, and this are skipped over in the compiling process. So, even though you have babel in your project, it's not compiling all your ES6 for some reason.

If you're using a different project that doesn't come with webpack/babel pre-setup, or wrote a custom setup for babel, that's where you should investigate first.

Comments

0

i think it is because your babel is not configured correctly.

4 Comments

I don't have to configure babel. That's the whole point of using create-react-app.
what OS do you use?
linux debian. How is it relevant?
if it is linux-like OS, it should not be any problems theoretically.

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.