使用 Typescript 创建 NextJS 应用程序的官方文档似乎不起作用
The official documentation for creating a NextJS app with Typescript doesn't seem to work
有一段时间我一直在尝试将 Typescript 用于我的 NextJS 项目...我认为开始使用 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 项目,我会遇到同样的错误。
请帮帮我。
这是一个已知错误,最新版本 create-next-app
不适用于 React 18
这里是官方问题:https://github.com/vercel/next.js/issues/36085
订阅以关注它
有一段时间我一直在尝试将 Typescript 用于我的 NextJS 项目...我认为开始使用 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 项目,我会遇到同样的错误。
请帮帮我。
这是一个已知错误,最新版本 create-next-app
不适用于 React 18
这里是官方问题:https://github.com/vercel/next.js/issues/36085
订阅以关注它