JSX for Django React教程上的“意外令牌”

时间:2019-02-20 05:39:12

标签: django reactjs webpack webpack-4

我一直遵循这个tutorial,并且终生无法弄清为什么webpack无法识别JSX?希望有人指出我想念的愚蠢的事情:)

我的完整代码在这里 https://github.com/dukarc/django-react-app

错误:

\django-react-app\leadmanager\frontend\src\components\App.js: Unexpected token (6:11)

  4 | class App extends Component {
  5 |   render() {
> 6 |     return <h1>React App</h1>;
    |            ^
  7 |   }
  8 | }
  • 我尝试过的事情

    1. rm -rf node_modules && rm -f package-lock.json && npm安装
    2. 参考了教程github,并再次检查了我是否处于同步状态
  • 版本(如果需要,将输出更多……这简直就是我的脑袋):

    • 节点:10.15.1
    • npm:6.8.0

package.json

{
  "name": "lead_manager_react_django",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development  ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js",
    "build": "webpack --mode production ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.3.1",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "webpack": "^4.29.0",
    "webpack-cli": "^3.2.1"
  },
  "dependencies": {
    "prop-types": "^15.6.2",
    "react": "^16.7.0",
    "react-dom": "^16.7.0"
  }
}

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["transform-class-properties"]
}

1 个答案:

答案 0 :(得分:3)

我签出了您的源代码,它有一个小的错字,是getElementById而不是getElementByID

以下文件中的第10行

https://github.com/dukarc/django-react-app/blob/master/leadmanager/frontend/src/components/App.js

它对我来说更正了。