12

Currently I can only use .js files, in case I try to use .jsx, this will cause an error. Here's the problem: I usually use code formatters, and writing jsx and formatting in js files, causes the code to be completely out of order, also causing syntax errors.

I am new to React, so I do not know how to configure to use jsx files, at least in other than the main files (index.js).

I've seen about the eject script, but there seem to be some drawbacks in using this.

Is there any solution for using jsx files without running "npm run eject"?

EDIT: The error is something like, "Could not import XXX file from filename.js", that is, when I try to import from a filename.jsx file, it searches only for filename.js files, and as there is no file. js, I can not care.

EDIT2: As said in a comment, just reboot the server, and I think I was changing from js to jsx when the server was already running, now I've changed and rebooted and it seems to be working

5
  • Why do you want to use jsx files at all? Commented Jun 8, 2019 at 18:40
  • 1
    Whats the error ? Is it editor's warning or programmatic error ? Please see your console. Commented Jun 8, 2019 at 18:44
  • @Robin, pls read the edit Commented Jun 8, 2019 at 19:31
  • which version of react-scripts do you have? Commented Jun 8, 2019 at 20:20
  • 1
    I tried from a fresh installation and worked out of the box, if I rename the extension of a file while the server is running I got an error, but after restarting the server works again, even if I mix .jsx and .js files Commented Jun 8, 2019 at 20:24

1 Answer 1

34

.jsx works out of the box with create react app.

You are changing the extensions while the server is running, so the server thinks that you are referencing a file that does not exist anymore since it resolves the file as .js initially and keeps that extension. It is like deleting a file and trying to import it--the same error is displayed.

Reboot the server and everything should work as expected.

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

1 Comment

Why does not create-react-app create App.js and index.js with jsx extentions from the beginning? Same for test files.

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.