模块构建失败:SyntaxError:C:/....../ main.jsx:意外的令牌

时间:2018-01-31 21:31:27

标签: javascript reactjs webpack babel

我已经被问了好几个小时了。每当我尝试运行WebPack时,它会给我模块构建错误,它会抱怨<。我在这个网站和其他网站上尝试了很多解决方案,但我仍然无法弄明白。任何帮助将不胜感激。

main.js

import Main from './main.jsx';

main.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import { Switch } from 'react-router';

class Nav extends React.Component {
  render() {
    return {
      <div>hello</div>
    }
  }
}

class App extends React.Compnent {
  render() {
    return {
      <div>
        <Nav />
      </div>
    }
  }
}

ReactDOM.render (
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById("mainContent")
);

webpack.config.js

const path = require('path');

module.exports = {
    entry: './js/main.js',
    output: {
        path: path.resolve(__dirname, 'docs'),
        filename: 'file.js'
    },
    devServer: {
      contentBase: path.join(__dirname, "docs"),
      port: 9000
    },
    module: {
      loaders: [
            { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
        ]
    },
};

也尝试了

    module: {
      loaders: [
            { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ , query: {presets:['es2015', 'react']} },
        ]

的package.json

{
  "name": "connectthrumusic",
  "version": "1.0.0",
  "description": "Shows the location of people around the world that are listening to the same song at the same time and allows you to chat with them.",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack-dev-server --open",
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/andrew2chan/ConnectThruMusic.git"
  },
  "author": "Andrew Chan",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/andrew2chan/ConnectThruMusic/issues"
  },
  "homepage": "https://github.com/andrew2chan/ConnectThruMusic#readme",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2016": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "webpack": "^3.10.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2016": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "webpack-dev-server": "^2.11.1"
  }
}

的index.html

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ConnectThruMusic</title>
</head>
<body>
<div id="mainContent"></div>
<script src="file.js" type="text/javascript"></script>
</body>
</html>

.babelrc

{
    "presets": ["env", "react", "es2015"]
}

Error

Files

1 个答案:

答案 0 :(得分:0)

简单地说是()而不是{}。感谢Sterling Archer指出这一点。