使用Typescript创建NextJS应用程序的官方文档似乎不起作用

vltsax25  于 2022-12-23  发布在  TypeScript
关注(0)|答案(2)|浏览(138)

我一直在尝试为我的NextJS项目使用Typescript...我以为开始使用Typescript会是最困难的,但事实并非如此:其安装甚至更加复杂。
我遵循了official documentation上的说明(创建一个新项目)。
以下是我所做的:

npx create-next-app --ts
cd test-nextts
npm run dev

下面是我在npm run dev上遇到的错误:

> test-nextts@0.1.0 dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install @types/react by running:

        npm install --save-dev @types/react

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).

它说我没有安装“所需的软件包”,而我安装了。下面是在npx create-next-app --ts之后自动生成的package.json

{
  "name": "test-nextts",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "12.1.4",
    "react": "18.0.0",
    "react-dom": "18.0.0"
  },
  "devDependencies": {
    "@types/node": "17.0.23",
    "@types/react": "18.0.2", // <--- HERE IT IS
    "@types/react-dom": "18.0.0",
    "eslint": "8.13.0",
    "eslint-config-next": "12.1.4",
    "typescript": "4.6.3"
  }
}

它要求我运行npm install --save-dev @types/react(即使我不应该这样做)。

up to date, audited 228 packages in 714ms

63 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

因此,我尝试了另一个npm run dev,问题是:它一遍又一遍地打印完全相同的错误!为什么?
如果我尝试修改现有的Javascript项目,也会出现同样的错误。
拜托,帮帮我。

de90aj5v

de90aj5v1#

这是一个已知漏洞,最新版本的create-next-app不适用于React 18
以下是官方发布的内容:https://github.com/vercel/next.js/issues/36085
订阅并关注它

9cbw7uwe

9cbw7uwe2#

考虑将Next.js更新到12或更高版本。另外,React版本应该是最新的。有一个关于升级的简短文档:
https://nextjs.org/docs/upgrading#upgrade-nextjs-version-to-12

相关问题