question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use `.tsx` or `.jsx` pages

See original GitHub issue

Environment

Nuxt CLI v3.0.0-27383920.81ee59c RootDir: /tmp/tmp.Qv46ef5Cmn/nuxt3-app Nuxt project info:


  • Operating System: Linux
  • Node Version: v17.3.0
  • Nuxt Version: 3.0.0-27383920.81ee59c
  • Package Manager: yarn@1.22.17
  • Bundler: Vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Create a JSX or TSX file in the pages folder and export a component from it. Example:

// pages/page.jsx
import { defineComponent } from "vue";

export default defineComponent({
  render(h) {
    return (
      <div>
        <h1>Test page</h1>
      </div>
    )
  }
});

Describe the bug

The CLI reports a warning when building. When the page is loaded in dev mode Vite reports an error and the page isn’t rendered.

The error being:

Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.

Additional context

I tried looking up if any additional configuration is needed for Nuxt to parse .jsx and .tsx files but found that it wasn’t for v2. I therefore presumed Nuxt 3 wouldn’t need any additional configuration either.

Logs

Build:

 WARN  [SSR] Error transforming /pages/page.jsx?macro=true: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.

  at formatError (node_modules/vite/dist/node/chunks/dep-f5552faa.js:36769:46)
  at TransformContext.error (node_modules/vite/dist/node/chunks/dep-f5552faa.js:36765:19)
  at TransformContext.transform (node_modules/vite/dist/node/chunks/dep-f5552faa.js:73659:22)
  at async Object.transform (node_modules/vite/dist/node/chunks/dep-f5552faa.js:36985:30)
  at async doTransform (node_modules/vite/dist/node/chunks/dep-f5552faa.js:52060:29)

Page load:

 ERROR  16:07:56 [vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
  Plugin: vite:import-analysis
  File: /tmp/tmp.Qv46ef5Cmn/nuxt3-app/pages/page.jsx?macro=true
  5  |      return (
  6  |        <div>
  7  |          <h1>Test page</h1>
     |                            ^
  8  |        </div>
  9  |      )
      at formatError (/tmp/tmp.Qv46ef5Cmn/nuxt3-app/node_modules/vite/dist/node/chunks/dep-f5552faa.js:36769:46)
      at TransformContext.error (/tmp/tmp.Qv46ef5Cmn/nuxt3-app/node_modules/vite/dist/node/chunks/dep-f5552faa.js:36765:19)
      at TransformContext.transform (/tmp/tmp.Qv46ef5Cmn/nuxt3-app/node_modules/vite/dist/node/chunks/dep-f5552faa.js:73659:22)
      at async Object.transform (/tmp/tmp.Qv46ef5Cmn/nuxt3-app/node_modules/vite/dist/node/chunks/dep-f5552faa.js:36985:30)
      at async doTransform (/tmp/tmp.Qv46ef5Cmn/nuxt3-app/node_modules/vite/dist/node/chunks/dep-f5552faa.js:52060:29)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
danielroecommented, Feb 28, 2022

Created an issue to track upstream: https://github.com/vitejs/vite/issues/7118.

0reactions
barylacommented, Feb 27, 2022

Having the same issue. It’s worth noting that /App.tsx works absolutely fine and so do /components/** if you import them inside /App.tsx. There just seems to be a problem with /pages.

What is indeed a little bit strange is that you can do this for a workaround until a fix:

// pages/index.tsx

import HomePage from '~/_pages';

export default HomePage;
// _pages/index.tsx

import { defineComponent } from 'vue';

export default defineComponent(() => {
  return () => <h1>inside _pages</h1>;
});

This does however show a warning in the browser console regarding Hydration text mismatch when I change the content of _pages/index.tsx. HMR works fine though, just the warning shows in the console:

runtime-core.esm-bundler.js:38 [Vue warn]: Hydration text mismatch:
- Client: "inside _pages!"
- Server: "inside _pages" 
  at <Anonymous onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {…}[[Handler]]: Object[[Target]]: Object[[IsRevoked]]: false > key="/" > 
  at <BaseTransition mode="out-in" appear=false persisted=false  ... > 
  at <Transition name="page" mode="out-in" > 
  at <RouterView > 
  at <NuxtPage> 
  at <Anonymous > 
  at <Anonymous > 
  at <NuxtLayout> 
  at <App> 
  at <NuxtRoot>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot use JSX unless the '--jsx' flag is provided
Cannot use JSX unless the '--jsx' flag is provided. Restart your IDE. Sometimes tsconfig.json changes aren't immediately picked up.
Read more >
Cannot use jsx unless the '--jsx' flag is provided (React)
To solve the error "Cannot use jsx unless the '--jsx' flag is provided", restart your IDE and development server and make sure your...
Read more >
Cannot use JSX unless the '--jsx' flag is ... - Super Code Bros
It made the problems in individual tsx files (3rd) 'go away', but trying to force a load after manually saving the flag as...
Read more >
Documentation - JSX - TypeScript
Using JSX with TypeScript. ... In order to use JSX you must do two things. ... Since the above syntax cannot be used...
Read more >
TypeScript and React using create-react-app - Level Up Coding
tsx . This is the extension that you should use anytime you include the JSX syntax in a React component file. You also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found