Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use JSX without React #15759

Open
tylerlong opened this issue Dec 14, 2024 · 3 comments
Open

Cannot use JSX without React #15759

tylerlong opened this issue Dec 14, 2024 · 3 comments
Labels
bug Something isn't working needs triage

Comments

@tylerlong
Copy link

tylerlong commented Dec 14, 2024

What version of Bun is running?

1.1.38

What platform is your computer?

Darwin 23.6.0 x86_64 i386

What steps can reproduce the bug?

Create demo.tsx:

/** @jsx h */
const h = () => "hello world";
console.log(<div></div>);

Run bun demo.tsx

What is the expected behavior?

It should print "hello world"

What do you see instead?

error: Cannot find module 'react/jsx-dev-runtime' from '/path/to/demo.tsx'

I just want to use JSX, I don't want to depend on React.

Additional information

This works like a charm:

tsc demo.tsx --jsx react
node demo.js

So tsc allows me to use JSX without React. The compiled code demo.js is:

/** @jsx h */
var h = function () { return "hello world"; };
console.log(h("div", null));

Bun should do the same instead of require 'react/jsx-dev-runtime'.
Because the code has nothing to do with React.

tsx works without any issue: tsx index.tsx.

@tylerlong tylerlong added bug Something isn't working needs triage labels Dec 14, 2024
@tylerlong tylerlong changed the title Cannot use jsx without React Cannot use JSX without React Dec 14, 2024
@woxxo
Copy link

woxxo commented Dec 14, 2024

On windows is a bit different:

bun demo.tsx downloads React automatically and outputs this:

{
  $$typeof: Symbol(react.transitional.element),
  type: "div",
  key: null,
  props: {},
  _owner: null,
  ref: null,
  _store: {
    validated: 0,
  },
  _debugInfo: null,
}

And if I try to build it:
bun build demo.tsx --outdir .\out

error: Could not resolve: "react/jsx-dev-runtime". Maybe you need to "bun install"?
    at path\to\demo.tsx

@tylerlong
Copy link
Author

tylerlong commented Dec 15, 2024

I find that the issue disappears if I create a tsconfig.json file with content:

{
  "compilerOptions": {
    "jsx": "react"
  }
}

A demo project: https://github.com/tylerlong/bun-jsx-without-react-demo

@woxxo
Copy link

woxxo commented Dec 16, 2024

if I create a tsconfig.json file with content:

Same result with bunfig.toml:

jsx = "react"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants