Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A .tsx file will not run with ts-node or plain Node.js

ErrorCannot run ts-node with .tsx files

Short 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.

  • Applies to ts-node 10, TypeScript 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.json
  2. 2Match what you see, then jump to its fix

    1. 1ts-node reports a compile error on the first JSX tagtsconfig.json has no jsx optionFix
    2. 2Files that contain JSX tags but end in .tsThe file that holds JSX has a .ts extensionFix
    3. 3node file.tsx fails while node file.ts worksYou run the file with plain node, which cannot strip .tsxFix

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.json

2Fix

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.tsx

Confirmed by typescriptlang.org

Did this fix it?

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=*.ts

2Fix

Rename the file to .tsx and update imports that name the extension.

git mv src/render.ts src/render.tsx

3Verify

Compile and run the renamed file.

npx ts-node src/render.tsx

Confirmed by typescriptlang.org

Did this fix it?

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 --version

2Fix

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.tsx

3Verify

The file runs and prints its output.

npx tsx src/render.tsx

Confirmed by nodejs.org

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.