添加组件链接错误后:未捕获的SyntaxError:意外的令牌导出

时间:2017-03-23 20:43:39

标签: javascript reactjs

我创建测试反应项目。添加模块和我的packege.json看起来像:

    {
  "name": "untitled",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "babel-preset-node5": "^12.0.1",
    "react-scripts": "0.9.5"
  },
  "dependencies": {
    "babel-preset-stage-0": "^6.22.0",
    "history": "^4.6.1",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-native": "^0.42.3",
    "react-router": "^4.0.0",
    "react-router-config": "^1.0.0-beta.1",
    "react-router-dom": "^4.0.0",
    "react-router-native": "^4.0.0",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "build": "babel input.js -o compiled.js"
  }
}

和webpack.config.js

var config = {
    entry: './index.js',

    output: {
        path:'/',
        filename: 'index.js',
    },

    devServer: {
        inline: true,
        port: 8080
    },

    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015', "node5", "stage-1", 'react', "stage-0"]
                }
            }
        ]
    }
};

module.exports = config;

在我的组件中i导入组件来自react-router-native的链接,此后添加react显示错误'Uncaught SyntaxError:Unexpected token export'但是如果我删除此字符串项目工作正常。它不是模块中唯一的组件,我可以添加任何组件,如Promt,Route或Router。为什么它不适用于Link?

这是重现错误的代码

import React from 'react';
import { Route, Router } from 'react-router';
import { Promt } from 'react-router';
import { Link } from 'react-router-native';

import logo from '../../logo.svg';
import './App.css';

class App extends React.Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

1 个答案:

答案 0 :(得分:0)

您需要将网络包配置中的test值的正则表达式更新为test: /(\.js|\.jsx)$/

现在你告诉webpack只通过babel loader运行.js个文件,但是你并没有告诉它也通过babel loader运行.jsx个文件。