-
-
Notifications
You must be signed in to change notification settings - Fork 658
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
ReferenceError: React is not defined when using hono/jsx in Nx-managed monorepo #3931
Comments
Hi @comatory Maybe this is not a bug. Just your setting is wrong. Can you share a minimal project to reproduce it on GitHub? |
@yusukebe Yeah it's quite possible it's my settings, but I'm using defaults and not changing a lot so it's odd. Here is repo where you can reproduce the problem: https://github.com/comatory/hono-nx-reference-error-react Use |
This patch will fix it. diff --git a/libs/app/tsconfig.lib.json b/libs/app/tsconfig.lib.json
index d997c1b..acd5ece 100644
--- a/libs/app/tsconfig.lib.json
+++ b/libs/app/tsconfig.lib.json
@@ -6,10 +6,16 @@
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
"emitDeclarationOnly": false,
- "types": ["node","hono"],
+ "types": [
+ "node",
+ "hono"
+ ],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
},
- "include": ["src/**/*.ts"],
+ "include": [
+ "src/**/*.ts",
+ "src/**/*.tsx"
+ ],
"references": []
-}
+}
\ No newline at end of file |
@yusukebe good catch, that settings should definitely be there. However, it still causes runtime error:
I applied the patch verbatim onto my repository as a new commit: comatory/hono-nx-reference-error-react@cebd72f Have you tried running it? Maybe I have different environment or perhaps you are invoking it differently than I am (I'm using |
I'm suspecting that this could also be an issue on |
Removing diff --git a/libs/app/tsconfig.lib.json b/libs/app/tsconfig.lib.json
index d997c1b..f49151a 100644
--- a/libs/app/tsconfig.lib.json
+++ b/libs/app/tsconfig.lib.json
@@ -4,12 +4,16 @@
"baseUrl": ".",
"rootDir": "src",
"outDir": "dist",
- "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
"emitDeclarationOnly": false,
- "types": ["node","hono"],
+ "types": [
+ "node",
+ "hono"
+ ],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
},
- "include": ["src/**/*.ts"],
+ "include": [
+ "src/**/*.ts"
+ ],
"references": []
-}
+}
\ No newline at end of file |
I tried removing that line and still get the same error. I have no |
This patch will resolve it. diff --git a/tsconfig.base.json b/tsconfig.base.json
index 8dcc429..3a472bd 100644
--- a/tsconfig.base.json
+++ b/tsconfig.base.json
@@ -13,7 +13,9 @@
"importHelpers": true,
"incremental": true,
"isolatedModules": true,
- "lib": ["es2022"],
+ "lib": [
+ "es2022"
+ ],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmitOnError": true,
@@ -29,6 +31,8 @@
"sourceMap": false,
"strict": true,
"target": "es2022",
- "verbatimModuleSyntax": false
+ "verbatimModuleSyntax": false,
+ "jsx": "react-jsx",
+ "jsxImportSource": "hono/jsx"
}
-}
+} |
Yes that will fix it, bit it is the solution I mentioned in my original post. It does not even need to be applied in
It is unusual solution because you're not supposed to put project specific compiler options to base TS configuration, since monorepo can contain all different sorts of projects. I'll try to ask in Nx forums/bug tracker how I could debug this issue and whether it's even related to Hono. |
I think Wrangler will refer to It's just a problem with the project setting for JSX. This is not a Hono-side bug. |
Yep, this is my suspicion as well. And that's completely fine that the tools look in default TS configuration. The issue seems that it does not follow project references for some reason and I think that is a bug. |
What version of Hono are you using?
4.6.14
What runtime/platform is your app running on? (with version if possible)
Cloudflare Workers
What steps can reproduce the bug?
npx [email protected] --skipGit --preset=ts --package-manager=pnpm
to generate Nxhono
as dependency:pnpm add [email protected] -w
wrangler
:pnpm add -D [email protected] -w
pnpm nx g @nx/js:lib libs/app --bundler=none --minimal
touch libs/app/wrangler.toml
with contents:libs/app/package.json
with target configuration under"nx"
key:libs/app/index.ts
tolibs/app/index.tsx
and replace contents:"compilerOptions"
key inlibs/app/tsconfig.lib.json
(this is recommended location for Nx projects):pnpm nx serve app
localhost:8787
and receive errorReferenceError: React is not defined
😭What is the expected behavior?
No error during runtime.
What do you see instead?
Application crashes with ReferenceError: React is not defined, see log. This happens both in dev mode and deployed application.
Editor is receiving errors from Typescript LSP next to JSX syntax:
Cannot find name 'React'
Additional information
I'm attaching several things:
tsconfig.json
andtsconfig.lib.json
of the generated Nx projectapp
tsconfig.base.json
which is the main TS config used by all the other projects in monorepoI am able to make the whole thing work by moving
"compilerOptions"
tolibs/app/tsconfig.json
(instead oftsconfig.lib.json
). However, this would be considered anti-pattern for Nx as you're supposed to adjust configuration for the program code in*.lib.json
config files (this avoids slow-downs once the monorepo gets too big).I realize that my issue can also be caused by mis-configuration in Nx, or some specific versions of tooling I'm using. But I considered there's a higher chance of someone noticing it here than in Nx bug tracker. But I will follow-up in Nx if needed but would really appreciate 2nd set of eyes on this.
tsconfig.json
tsconfig.lib.json
tsconfig.base.json
The text was updated successfully, but these errors were encountered: