[英]React app works fine when running with react-scripts start but gets a "Unexpected SyntaxError: Unexpected Token: <" when built
[英]React-scripts - Syntax Error: Unexpected token {
我一直在按照本指南從 react-scripts-typescript 升級到 react-scripts 並升級我的腳本以使用 react-scripts。
運行npm run start
會產生以下錯誤:
node_modules/webpack-dev-server/lib/Server.js:348
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/..mypath/node_modules/react-scripts/scripts/start.js:28:26)
以下是我使用的版本:
Package.json文件:
{
"dependencies": {
"@types/node": "^17.0.1",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"@types/react-redux": "^7.1.5",
"apollo-upload-client": "^10.0.1",
"autotrack": "^2.4.1",
"core-js": "^3.6.5",
"d3": "^5.9.2",
"date-fns": "^2.8.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"fetch-mock": "^6.5.2",
"file-saver": "^1.3.3",
"graphql": "^15.5.0",
"graphql-tag": "^2.12.4",
"history": "^5.0.0",
"https-localhost": "^4.6.2",
"material-ui-next-pickers": "0.0.19",
"mdi-material-ui": "^5.1.1",
"moment": "^2.24.0",
"nivo": "^0.31.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-https-redirect": "^1.1.0",
"react-redux": "^5.0.7",
"react-router": "^6.0.0-beta.0",
"react-router-dom": "^6.0.0-beta.0",
"react-scripts": "^5.0.0",
"redux-mock-store": "^1.5.3",
"redux-saga": "^0.1å6.2",
"redux-saga-test-plan": "^3.7.0",
"reselect": "^4.0.0",
"serve": "^11.3.2",
"typescript": "^4.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "... react-scripts build",
"test": "react-scripts test --env=jsdom --transformIgnorePatterns ....",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"engines": {
"node": "16.13.1"
},
"devDependencies": {}
}
tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"target": "es5",
"lib": ["dom", "esnext"],
"sourceMap": true,
"allowJs": true,
"jsx": "react-jsx",
"moduleResolution": "node",
"rootDir": "",
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"alwaysStrict": false,
"strictFunctionTypes": false,
"strictPropertyInitialization": false
}
}
不確定錯誤來自哪里,但這就是我所使用的。 期待命令 npm start 啟動反應應用程序。
即使沒有導入 react17 的特性 React,代碼也能正常工作。
我有同樣的問題,對我來說這是因為我重新啟動了我的計算機並且 NVM 將我重置到節點 8,使用 nvm 恢復到 14 為我修復了它。 建議您確認您的節點版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.