简体   繁体   English

react-scripts中随机位置的“语法错误:意外关键字”

[英]“Syntax error: Unexpected keyword” in random places in react-scripts

I've recently updated my Javascript project from Webpack to react-scripts. 我最近将我的Javascript项目从Webpack更新为react-scripts。

My code structure is fairly simple. 我的代码结构非常简单。 I have a src folder with an index.js that just renders the DOM like this: 我有一个带有index.jssrc文件夹,它仅以如下方式呈现DOM:

import ReactDOM from 'react-dom';
import Index from './pages/index';

ReactDOM.render(<Index />, document.querySelector('#root'));

and my Index which is just a single page that renders some stuff like this: 而我的Index只是一个页面,呈现了如下内容:

/**
 * Injected styles for this component
 */
const styles = theme => ({
  ...
})
class Index extends Component {
  ...
}

export default withRoot(withStyles(styles)(Index));

in my package.json I use react-scripts to start the app. 在我的package.json我使用react-scripts启动应用程序。 When running npm run start the dev-webserver starts. 在运行npm run startnpm run start dev-webserver。

I can change a single letter, save the file, the dev-webserver restarts, and then I get random syntax errors throughout the code. 我可以更改一个字母,保存文件,重新启动dev-webserver,然后在整个代码中出现随机语法错误。 They look like this: 他们看起来像这样:

./src/pages/index.js
Syntax error: Unexpected keyword 'return' (144:7)

  142 |
  143 |     if(!this.state.data){
> 144 |       return null;
      |       ^
  145 |     }
  146 |
  147 |     return <Grid>

or this 或这个

./src/pages/index.js
Syntax error: Unexpected token (76:11)

  74 |    */
  75 |   render() {
> 76 |     const { classes } = this.props;
     |           ^
  77 |
  78 |     return (
  79 |       <div className={classes.root}>

or on any other part of the project. 或项目的其他任何部分。 They keep happening until I restart npm. 它们一直发生,直到我重新启动npm。

I've tried to delete code until it doesn't happen anymore. 我试图删除代码,直到不再发生。 Then I end up with a single React.Component that only renders a div with text. 然后,我得到了一个只用文本渲染div的React.Component

I've tried to work on another project; 我尝试过另一个项目。 the same issue happens there. 同样的问题在那里发生。

The project works fine on other devices. 该项目可以在其他设备上正常运行。

Things I've tried: 我尝试过的事情:

  • delete node_modules folder 删除node_modules文件夹
  • downgrade dependencies 降级依赖
  • delete project and clone again 删除项目并再次克隆
  • use different browser 使用其他浏览器
  • restart pc 重新启动电脑
  • change back to webpack 改回webpack
  • try to search online for the error 尝试在线搜索错误

My node version is: 我的节点版本是:

v6.9.1 v6.9.1

My npm version is: 我的npm版本是:

v6.8.0 v6.8.0

Turns out my node version is very old (2016) . 原来我的节点版本很旧(2016年)

I updated node to the newest version and it stopped happening. 我将节点更新为最新版本,并且它停止发生。

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

相关问题 React-react-scripts publicPath - React - react-scripts publicPath create-react-app 错误:找不到模块“react-scripts/scripts/init.js” - create-react-app Error: Cannot find module 'react-scripts/scripts/init.js' react-scripts babel 脚本在本地反应应用程序上使用纱线运行时出错 - react-scripts babel scripts error running with yarn on local react application 错误:尝试安装 react 应用程序时找不到模块“react-scripts/scripts/init.js” - Error: cannot find module "react-scripts/scripts/init.js' when tryping to install react app React 应用程序在使用 react-scripts start 运行时工作正常,但在构建时出现“意外语法错误:意外令牌:<” - React app works fine when running with react-scripts start but gets a "Unexpected SyntaxError: Unexpected Token: <" when built react-scripts 启动错误(找不到模块'../scripts/start') - 我该如何解决这个问题? - react-scripts start error (Cannot find module '../scripts/start') - how can i fix this? 在 CRA/react-scripts 中使用可选链接 (?.) - using optional chaining(?.) with CRA/react-scripts 没有使用 react-scripts 和 jest 找到测试 - No tests found using react-scripts and jest 了解 react-scripts 如何开始工作 - Understanding how react-scripts start work npm 安装:反应脚本安装失败 - npm install: react-scripts install fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM