Skip to content

Instantly share code, notes, and snippets.

@parties
Last active January 12, 2026 16:54
Show Gist options
  • Select an option

  • Save parties/90cdf35f9a3d05bea6df76dc83a69641 to your computer and use it in GitHub Desktop.

Select an option

Save parties/90cdf35f9a3d05bea6df76dc83a69641 to your computer and use it in GitHub Desktop.
rename all *.js files containing React markup to *.jsx
# finds all *.js files that have either `</` or `/>` tags in them and renames them to *.jsx
find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l -E "</|/>" "$0"' {} \; -exec bash -c 'mv "$0" "${0%.js}.jsx"' {} \;
@wafaa-ismail

Copy link
Copy Markdown

Nice

@parties

parties commented May 16, 2023

Copy link
Copy Markdown
Author

Updated to also check for self-closing tags (/>), thank you @sean-roberts!

@jsakas I decided not to change it to git mv because it will throw errors if the files aren't under source control, but it's an easy change to make either way. Thanks for calling it out!

@SaadBazaz

Copy link
Copy Markdown

Works really well. Good job!

@Dev-Zhao

Dev-Zhao commented Jul 26, 2023

Copy link
Copy Markdown

@parties Thanks for the snippet

The command doesn't work if the file or any of its parent folders have names with spaces in them.
/src/pages/Folder With Spaces/Another Folder With Spaces/file.js

To fix this we need to add quotes around $0 for grep: grep -l -E "</|/>" $0 -> grep -l -E "</|/>" "$0"

find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l -E "</|/>" "$0"' {} \; -exec bash -c 'git mv "$0" "${0%.js}.jsx"' {} \;

@parties

parties commented Aug 22, 2023

Copy link
Copy Markdown
Author

Updated to handle spaces, thank you @Dev-Zhao!

@ratelChief

Copy link
Copy Markdown

this one also renames a *.test.js files to *.test.jsx so it could impact the tests

@petemihaylov

Copy link
Copy Markdown

Thanks!

@skandog

skandog commented Oct 24, 2023

Copy link
Copy Markdown

Thank you!

@marlo22

marlo22 commented Jan 11, 2024

Copy link
Copy Markdown

A big time saver, thank you. 🙂

@faridescate

Copy link
Copy Markdown

Thanks!

@yasminadelia

Copy link
Copy Markdown

Thank you so much!

@INR-L

INR-L commented Mar 26, 2024

Copy link
Copy Markdown

Whats is the beinfits after changin this, how to prevent error in pollyfill webpack 5

@Dev-Zhao

Dev-Zhao commented Mar 26, 2024

Copy link
Copy Markdown

@inrl-md This is used when you have something like: https://vitejs.dev/ to run your React app instead of create-react-app. Vite requires any files containing jsx code to have the .jsx file extension. Vite uses a preconfigured Rollup as the bundler which is more efficient instead of Webpack (used by create-react-app). Changing .js to .jsx does not help resolve any errors in polyfill for Webpack 5.

@INR-L

INR-L commented Mar 27, 2024

Copy link
Copy Markdown

@inrl-md This is used when you have something like: https://vitejs.dev/ to run your React app instead of create-react-app. Vite requires any files containing jsx code to have the .jsx file extension. Vite uses a preconfigured Rollup as the bundler which is more efficient instead of Webpack (used by create-react-app). Changing .js to .jsx does not help resolve any errors in polyfill for Webpack 5.

Bro how to fix polyfill for Webpack 5.

I tried many different methods for 2 days, still error

i want to run nodemailer on my react project

@Dev-Zhao

Dev-Zhao commented Mar 27, 2024

Copy link
Copy Markdown

@inrl-md All this gist does is change .js files containing jsx code to have the .jsx extension. If you need help with polyfill for webpack 5, you can post a question on stackoverflow or open an issue in the webpack repo. No one will know why your webpack is not working when you have not provided any information. I suggest setting up your react project in a https://codesandbox.io/ and try to reproduce the error, so someone can help debug it.

@shiva-69

Copy link
Copy Markdown

thanks, it worked

@vitld

vitld commented May 20, 2024

Copy link
Copy Markdown

Does not seem to catch files that only have custom react hooks defined. But maybe those do not need to be .jsx? (In context of Vite).

@Dev-Zhao

Dev-Zhao commented May 21, 2024

Copy link
Copy Markdown

@ViktorPontinen only files containing jsx code need to be .jsx (for vite)

@d4nielchaves

Copy link
Copy Markdown

Thank you!

@TimKochDev

Copy link
Copy Markdown

Us poor windows users can use the following powershell command:

Get-ChildItem -Path ./src -Filter *.js -Recurse | Where-Object { 
    $_.Name -notlike '*.jsx' -and $_.Name -notlike '*.ejs' 
} | ForEach-Object {
    $fileContent = Get-Content $_.FullName
    if ($fileContent -match '</|/>') {
        $newFileName = $_.FullName -replace '\.js$', '.jsx'
        Rename-Item -Path $_.FullName -NewName $newFileName
    }
}

(The code above is ai generated but worked flawlessly for me. The matching logic should be the same as in the original bash script.)

@famgz

famgz commented Apr 3, 2025

Copy link
Copy Markdown

Thanks

@AmolShelke2

Copy link
Copy Markdown

Us poor windows users can use the following powershell command:

Get-ChildItem -Path ./src -Filter *.js -Recurse | Where-Object { 
    $_.Name -notlike '*.jsx' -and $_.Name -notlike '*.ejs' 
} | ForEach-Object {
    $fileContent = Get-Content $_.FullName
    if ($fileContent -match '</|/>') {
        $newFileName = $_.FullName -replace '\.js$', '.jsx'
        Rename-Item -Path $_.FullName -NewName $newFileName
    }
}

(The code above is ai generated but worked flawlessly for me. The matching logic should be the same as in the original bash script.)

works fine thanks mate

@dan-diaz

Copy link
Copy Markdown

I have some test suite files that contain string HTML, but not React/JSX and they were caught up in this. Just be careful where you run this script

expect(noteContent).toBe('<p>Hello World</p>');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment