create-react-app打字稿无法运行npm start [英] create-react-app typescript won't run npm start

查看:128
本文介绍了create-react-app打字稿无法运行npm start的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用打字稿 create-react-app ,但是JSX和TS似乎存在问题.我读了一些可能的解决方案,但这些解决方案对我来说并不可行.

I am trying to create-react-app with typescript, but there seems to be a problem with JSX and TS. I read a couple of possible solutions, that didn't work out for me.

我有:

  • npm:版本16.14.7
  • 节点:版本14.8.0

我从安装正常,但是当我尝试运行 npm start 时,出现此错误:

The installation went fine, but when I try to run npm start it gives me this error:

> my-app@0.1.0 start /Users/lukasbenediktson/Desktop/react-types/my-app
> react-scripts start

/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'
    at verifyTypeScriptSetup (/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)
    at Object.<anonymous> (/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1251:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
    at Module.load (internal/modules/cjs/loader.js:1100:32)
    at Function.Module._load (internal/modules/cjs/loader.js:962:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the my-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我试图在 node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js 中将此行238更改为

I tried to change this line 238 in node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js as suggested by tudor07 here:

} else if (parsedCompilerOptions[option] !== valueToCheck) {

} else if (parsedCompilerOptions[option] !== valueToCheck && option !== "jsx") {

,然后让我运行 npm start ,服务器已启动并正在运行.但现在它表明它无法识别JSX/TSX:

and that let me run npm start and the server is up and running. But it now reveals that it won't recognize JSX/TSX:

TypeScript error in /Users/lukasbenediktson/Desktop/react-types/my-app/src/App.tsx(7,5):
Could not find a declaration file for module 'react/jsx-runtime'. '/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react/jsx-runtime.js' implicitly has an 'any' type.
  If the 'react' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react`  TS7016

     5 | function App() {
     6 |   return (
  >  7 |     <div className="App">
       |     ^
     8 |       <header className="App-header">
     9 |         <img src={logo} className="App-logo" alt="logo" />
    10 |         <p>

我的 package.json 看起来像这样:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-scripts": "4.0.0",
    "typescript": "^4.0.3",
    "web-vitals": "^0.2.4"
  },
  "devDependencies": {
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.53",
    "@types/react-dom": "^16.9.8"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

谢谢您的时间.

更新:解决方案(至少对我来说)

在我的 tsconfig.json 中,将我的"compilerOptions":"jsx":"react-jsx" 更改为改为"jsx":"react" .请注意,我仍然需要在 node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js 中的第238行进行更改,更改为

In my tsconfig.json I changed the my "compilerOptions": from "jsx": "react-jsx" to "jsx": "react". Notice that I still needed to make a change on line 238 in my node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js as suggested by tudor07 here

请让我知道它是否太"hacky"

Please let me know if it's too "hacky"

推荐答案

在我的情况下,@ captain-yossarian正确.给定指定版本的React( react@17.0.1 )的过时类型( @ types/react @ 16.9.4 ).要解决:

In my case @captain-yossarian had it right. Out of date types (@types/react@16.9.4) given the specified version of React (react@17.0.1). To fix:

npm install --upgrade @types/react@17

这篇关于create-react-app打字稿无法运行npm start的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆