NextJS cannot recognize TypeScript files

Tags: , , ,



I’m newbie to NextJS.
Anyway, I want to develop my website using TypeScript not a JS. So I followed NextJS’s official to install TS from scratch, but when I run npm run dev, 404 Error page greeting me.
OK below is my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

Hmm, Nothing seems to be wrong I think 🙁
And then next is package.json.

{
  "name": "MY_PROJECT_NAME",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "go": "next build && next start",
    "debug": "NODE_OPTIONS='--inspect' next dev"
  },
  "dependencies": {
    "@mdx-js/loader": "^1.6.22",
    "@next/mdx": "^10.0.3",
    "@types/node": "^14.14.14",
    "@types/react": "^17.0.0",
    "@types/styled-components": "^5.1.5",
    "@types/webpack": "^4.41.25",
    "autoprefixer": "^10.1.0",
    "express": "^4.17.1",
    "gray-matter": "^4.0.2",
    "next": "^10.0.3",
    "postcss": "^8.2.1",
    "raw-loader": "^4.0.2",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-markdown": "^4.3.1",
    "remark-emoji": "^2.1.0",
    "remark-html": "^13.0.1",
    "remark-images": "^2.0.0",
    "styled-components": "^5.2.1",
    "tailwindcss": "^2.0.2",
    "typescript": "^4.1.3",
    "webpack": "^4.44.0"
  }
}

Hmm, what’s wrong with my codes?
When I run my codes as .jsx, All of things are normally works. But .tsx is NOT WORKING.
Does any one know about this problem?
Please help me please!

— Add index.tsx —

import Layout from '../components/Layout'

export default () => (
    <>
        <Layout title="Kreimben::Home" isHome={true}>
            <div className="flex justify-center text-center">
                <div className="bg-gray-300 p-8 m-12 rounded-lg w-4/5 text-4xl font-serif">
                                Welcome to indie developer's website!
                </div>
            </div>
            <main className="flex justify-center">
                <div className="w-4/5 py-32 mb-12 shadow-xl rounded-lg bg-gradient-to-r from-teal-300 to-blue-500 text-center">
                    <p className="text-4xl font-serif">I code</p>
                    <p className="font-semibold text-6xl">iOS, macOS, and anything!</p>
                </div>
            </main>
        </Layout>
    </>
)

Answer

I’ve just figured out the issue from @next/mdx plugin configuration. The pageExtensions is supposed to be [ts, tsx] as you switched to Typescript.

In short, refine the correct ext files would fix the issue:

next.config.js

// ...

module.exports = withMDX({
  pageExtensions: ['ts', 'tsx', 'md', 'mdx'], // Replace `jsx?` with `tsx?`
})



Source: stackoverflow