找不到所需的文件。- 将 TypeScript 添加到 React 项目

IT技术 reactjs typescript
2021-05-01 13:57:47

尝试通过以下方式将 TypeScript 添加到已创建的 React 应用程序:https : //create-react-app.dev/docs/adding-typescript

当我尝试启动时,我做了所有事情,但出现以下错误:

> npx react-scripts start
Could not find a required file.
  Name: index.js
  Searched in: /home/karl/dev/afry/tmr-client/src
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! tmr-client@0.1.0 start: `npx react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the tmr-client@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/karl/.npm/_logs/2020-11-06T18_43_02_622Z-debug.log

我按照指南中的说明将 index.js 重命名为 index.ts。

2个回答

我今天在使用 create-react-app 时遇到了同样的问题。问题是根据这个 Git 问题的 react-scripts 版本:Git Reference

一个快速的解决方案是更新 react-scripts。我正在使用 react-scripts 4.0.3 版,没有任何问题。

一种基于文档的安全方法:创建 React App Docs 是 将 package.json 文件中的 react-scripts 版本更改为最新的稳定版本,例如 "react-scripts": "^4.0.3" . 然后,从命令行运行npm install

TLDR : npm i -D react-scripts@latest

过时的 react-scripts 版本

我不确定为什么index.ts不适合你,它似乎受支持

唯一的解释是您使用的是过时版本的react-scripts,您只需运行npm i -D react-scripts@latest. 更新到新版本的更多细节

Create React App 分为两个包:

create-react-app 是一个全局命令行实用程序,可用于创建新项目。react-scripts 是生成的项目(包括这个)中的一个开发依赖项。当您运行 npx create-react-app my-app 时,它会自动安装最新版本的 Create React App。

如果您之前已通过 npm install -g create-react-app 全局安装 create-react-app,请访问入门以了解当前的安装步骤。

Create React App 使用最新版本的 react-scripts 创建项目,因此您将自动获得新创建的应用程序中的所有新功能和改进。

要将现有项目更新为新版本的 react-scripts,请打开更改日志,找到您当前使用的版本(如果不确定,请检查此文件夹中的 package.json),然后应用更新的迁移说明版本。

在大多数情况下,在 package.json 中修改 react-scripts 版本并在该文件夹中运行 npm install(或 yarn install)应该就足够了,但最好查阅更改日志以了解潜在的重大更改。

我们Promise将破坏性更改保持在最低限度,以便您可以轻松升级 react-scripts。

如果这是问题所在,则您依赖于全局安装create-react-app- 不建议这样做,因为它不会使用最新版本。请参阅快速入门下的注释

如果您之前已通过 npm install -g create-react-app 全局安装 create-react-app,我们建议您使用 npm uninstall -g create-react-app 或 yarn global remove create-react-app 卸载包以确保npx 总是使用最新版本。