React + Webpack + babel7无法解析es6样式函数分配

时间:2018-10-26 08:15:15

标签: node.js reactjs npm ecmascript-6 babel

我正在学习React(重复课程)。我陷入困境,试图升级我的班级项目以使用ES6样式函数声明和赋值。是的,我试图找到一个类似的问题,但没有找到任何有效的方法。

这是我的package.json:

    {
  "name": "redux-simple-starter",
  "version": "1.0.0",
  "description": "Simple starter package for Redux with React and Babel support",
  "main": "index.js",
  "repository": "git@github.com:StephenGrider/ReduxSimpleStarter.git",
  "scripts": {
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch",
    "build": "webpack -p --define process.env.NODE_ENV='\"production\"' --progress --colors"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "7.0.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.4",
    "babel-preset-env": "^1.7.0",
    "webpack": "^2.7.0",
    "webpack-dev-server": "^1.16.5"
  },
  "dependencies": {
    "lodash": "^3.10.1",
    "react": "16.3.2",
    "react-dom": "16.3.2",
    "react-redux": "5.0.7",
    "redux": "4.0.0",
    "yarn": "^1.10.1",
    "youtube-api-search": "0.0.5"
  }
}

和我的.babelrc文件:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

还有我的webpack.config.js:

module.exports = {
  entry: ['./src/index.js'],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: ["@babel/preset-env","@babel/preset-react"]
        }
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './',
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000
    }
  }
};

主要的index.js文件如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import SearchBar from './components/search_bar';

const API_KEY = 'xxx';

const App = function() {
  return (
    <div>
      <SearchBar />
    </div>
  );
}
ReactDOM.render(
  <App />, document.querySelector('.container'));

最后,search_bar.js文件如下所示:

import React from 'react';

const SearchBar = () ==> {
  return <input />;
}

export default SearchBar;

我已经完成了npm安装,等等,但是当我使用npm start运行应用程序时,出现错误:

./src/components/search_bar.js中的

错误 模块构建失败:SyntaxError:/Users/xxxxx/ReactProjects/ReduxSimpleStarter/src/components/search_bar.js:意外令牌(3:19)

1 |从'react'导入React;   2 |

  

3 | const SearchBar =()==> {       | ^     4 |回报;     5 | }     6 |       在_class.raise(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:3939:15)       在_class.unexpected(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5248:16)       在_class.parseParenAndDistinguishExpression(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:6509:12)       在_class.parseExprAtom(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:6284:21)       在_class.parseExprAtom(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:3635:52)       在_class.parseExprSubscripts(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5924:21)       在_class.parseMaybeUnary(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5903:21)       在_class.parseExprOps(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5812:21)       在_class.parseMaybeConditional(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5784:21)       在_class.parseMaybeAssign(/Users/xxxxx/ReactProjects/ReduxSimpleStarter/node_modules/@babel/parser/lib/index.js:5731:21)    @ ./src/index.js 3:0-48    @多./src/index.js   webpack:编译失败。

任何人都可以帮助解释这里出了什么问题以及为什么它不起作用吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您在=中有两个const SearchBar = () ==>