A .tsx file will not run with ts-node or plain Node.js
Cannot run ts-node with .tsx filesShort answer
JSX needs two things: a .tsx file name and a jsx value in compilerOptions. Without them TypeScript cannot compile the file. If you run it with plain node instead of ts-node, type stripping does not support .tsx at all, so use ts-node with a jsx setting or the tsx runner.
Fix it fast
1Find out which cause you have
Check how the file is started and whether tsconfig.json sets jsx. The command decides which cause below applies.
grep -n "jsx" tsconfig.json grep -n -E "ts-node|node " package.json2Match what you see, then jump to its fix
What does the error look like?
Cannot run ts-node with .tsx files.tsx files are unsupported (Node.js type stripping)
Cause 1 of 3
tsconfig.json has no jsx option
1Check
TypeScript compiles JSX only when the jsx option is enabled, from tsconfig.json or the command line.
grep -n "jsx" tsconfig.json2Fix
Add a jsx mode that fits your code. react-jsx uses the automatic runtime and needs no React import in each file.
{
"compilerOptions": {
"jsx": "react-jsx"
}
}3Verify
Run the file again.
npx ts-node src/render.tsxConfirmed by typescriptlang.org
Cause 2 of 3
The file that holds JSX has a .ts extension
1Check
TypeScript allows JSX only in files named .tsx.
grep -rln "</" src --include=*.ts2Fix
Rename the file to .tsx and update imports that name the extension.
git mv src/render.ts src/render.tsx3Verify
Compile and run the renamed file.
npx ts-node src/render.tsxConfirmed by typescriptlang.org
Cause 3 of 3
You run the file with plain node, which cannot strip .tsx
1Check
Node.js runs .ts files by removing types, but its documentation states that .tsx files are unsupported, and tsconfig.json is ignored.
node --version2Fix
Use a runner that compiles JSX: ts-node with the jsx option above, or the tsx package the Node.js docs recommend.
npx tsx src/render.tsx3Verify
The file runs and prints its output.
npx tsx src/render.tsxConfirmed by nodejs.org
How do I stop it coming back?
- Set jsx in tsconfig.json when you create a project that holds React code.
- Use the .tsx extension for every file with JSX.
- Do not rely on plain node for files that need a compile step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
ts-node fails after switching to type module and module ESNext
ERR_REQUIRE_ESM
Setting type to module makes Node load your files as native ESM, and plain ts-node does not hook into that loader.
4 causes, each with a fixBack end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fix