gpt4 book ai didi

reactjs - create-react-app typescript 不会运行 npm start

转载 作者:行者123 更新时间:2023-12-04 15:12:43 34 4
gpt4 key购买 nike

我正在尝试 create-react-app用 typescript ,但 JSX 和 TS 似乎有问题。我阅读了几个可能的解决方案,但对我来说没有用。
我有:

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

  • 我用过 npx create-react-app my-app --template typescript来自 docs
    安装进行得很顺利,但是当我尝试运行 npm start 时它给了我这个错误:
    > 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如 suggested by tudor07 here :
    } else if (parsedCompilerOptions[option] !== valueToCheck) {
    } else if (parsedCompilerOptions[option] !== valueToCheck && option !== "jsx") {
    这让我跑 npm start并且服务器已启动并正在运行。但现在显示它无法识别 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 行进行更改。如 suggested by tudor07 here
    如果它太“hacky”,请告诉我

    最佳答案

    就我而言,@captain-yossarian 是对的。给定指定版本的 React ( @types/react@16.9.4 ) 的过时类型 ( react@17.0.1 )。修理:

    npm install --upgrade @types/react@17

    关于reactjs - create-react-app typescript 不会运行 npm start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64926632/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com