简体   繁体   English

为什么将 app.js 更改为 app.tsx 会破坏我的 Next.js 项目?

[英]Why does changing app.js to app.tsx break my Next.js project?

I'm trying to set up the Next.js framework with TypeScript, and I'm able to change my pages/index.js page to an pages/index.tsx page without issue, but when I attempt to change pages/_app.js to pages/_app.tsx , the project breaks with the error:我正在尝试使用 TypeScript 设置 Next.js 框架,并且可以毫无问题地将我的pages/index.js页面更改为pages/index.tsx页面,但是当我尝试更改pages/_app.jspages/_app.tsx ,项目因错误而中断:

Error: ENOENT: no such file or directory, open /../../pages/_app.js错误:ENOENT:没有/../../pages/_app.js文件或目录,打开/../../pages/_app.js

Why is this happening?为什么会这样? I've installed TypeScript, @types/react, and @types/node我已经安装了 TypeScript、@types/react 和 @types/node

You may do the following您可以执行以下操作

  1. Close the script ie npm run dev关闭脚本即npm run dev
  2. Delete .next folder删除 .next 文件夹
  3. Run the app again ( npm run dev ).再次运行应用程序 ( npm run dev )。 It should work now.它现在应该可以工作了。

I think it is a problem with caching我认为这是缓存的问题

Create an empty tsconfig.json file in the root of your project.在项目的根目录中创建一个空的tsconfig.json文件。

The then run run next dev然后运行run next dev

  1. Stop the script from running ctrl+c (Mac) on terminal.停止脚本在终端上运行 ctrl+c (Mac)。

  2. Write this in _app.tsx: export default function App({ Component, pageProps }: AppProps): JSX.Element { return <Component {...pageProps} /> } _app.tsx 中这样写: export default function App({ Component, pageProps }: AppProps): JSX.Element { return <Component {...pageProps} /> }

  3. Re-run the script npm run dev重新运行脚本 npm run dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM