if you are only using babel to transform jsx to js, this is what you need :
installation
- install
babel-clias global (optional)sudo npm install -g babel-cli - install
babel-preset-es2015(optional. if your code using es6 standard)npm install babel-preset-es2015 - install
babel-preset-react(required)
configuration
in your root of project, add this file .babelrc and write this to it
{
"presets": ["es2015", "react"]
}
or
{
"presets": ["react"]
}
run
babel source_dir -d target_dir
Answer from yussan on Stack OverflowHello everyone, I have been using .jsx this whole time when working with ReactJS but yesterday I thought of actually figuring out the difference and upon searching on internet it shows that creating html elements like (div, p, h1, etc) are convenient as the syntax is same and you’ve just put that inside “return”. But the same can be done inside .js, I was checking some code and saw devs using .js almost similar to .jsx and creating html inside it the same way it’s done in .jsx by putting things inside return statement.
Now at this point I am bit confused and really want someone to clarify the difference.
if you are only using babel to transform jsx to js, this is what you need :
installation
- install
babel-clias global (optional)sudo npm install -g babel-cli - install
babel-preset-es2015(optional. if your code using es6 standard)npm install babel-preset-es2015 - install
babel-preset-react(required)
configuration
in your root of project, add this file .babelrc and write this to it
{
"presets": ["es2015", "react"]
}
or
{
"presets": ["react"]
}
run
babel source_dir -d target_dir
As of 2023 if you are using babel to transform jsx to js, this is what you will need to do:
Install Packages
Install the following globally on your machine:
npm install -g @babel/cli
npm install -g @babel/core
npm install -g @babel/preset-react
or install the following as a project dependency (my personal preference):
npm install --save-dev @babel/cli
npm install --save-dev @babel/core
npm install --save-dev @babel/preset-react
Create Configuration File
Create a .babelrc config file at the root of your project with the following setting as a bare minimum:
{
"presets": ["@babel/preset-react"]
}
You should review the babel docs if you have a more complicated use case and determine what additional settings you may need to add. You can find the settings for compiling jsx to js on the @babel/preset-react page.
Compile
You can compile any jsx file into js by running the following command in your terminal:
npx babel [jsx-file-here.jsx] -d [output-directory]
If your jsx file imports anything please keep the following in mind:
- This will not import any imported code. You will need to also compile any files that are imported. You can then use your code as a browser module: see next 2 points.
- JS imports are widely supported now (see also) but you will need to add
type="module"to the script tag in your HTML. - Remember you must include the file extension
.jsin your import statements still! No browser will intuptret import Foo from './bar' asimport Foo from './bar.js. - If you do not want to do the previous you can use something like rollup.js to bundle all the files into a single script.
I'm new to React JS and I've noticed that when I create a React app, it generates JS files instead of JSX.
I'm curious about the differences between these two file extensions and what most people prefer to use.
Can anyone explain this to me?
Thank you!