React | Webpack4 | Babel7模块构建失败

时间:2018-09-26 17:55:56

标签: reactjs babel webpack-4

我正在尝试使用webapck4和babel7设置一个react项目,但出现以下错误。

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/ritesh/myprojects/testproj/reduxspa/js/jsapp/src/index.js: Unexpected token (7:2)

   5 | 
   6 | ReactDOM.render(
>  7 |   <div>{title}</div>,
     |   ^
   8 |   document.getElementById('app')
   9 | );
  10 | 
    at Parser.raise (/home/ritesh/myprojects/testproj/reduxspa/js/jsapp/node_modules/@babel/parser/lib/index.js:3939:15)

下面是我的设置和文件详细信息,而jsapp是我的根文件夹。

我的操作系统是Ubuntu16。

dist / index.html

<!DOCTYPE html>
<html>
<head>
    <title>test html</title>
</head>
<body>
    <div id='app'></div>
    <script type="text/javascript" src="./bundle.js"></script>
</body>
</html>

src / index.js

import React from 'react';
import ReactDOM from 'react-dom';

const title = 'My Minimal React Webpack Babel Setup';

ReactDOM.render(
  <div>{title}</div>,
  document.getElementById('app')
);

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',

  output: {
    filename: 'bundle.js',
    publicPath: '/',
    path: path.resolve(__dirname, 'dist'),
  },

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

  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
};

.babelsrc

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

package.json

{
  "name": "jsapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.2",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.9"
  },
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2"
  }
}

我不确定自己在做什么错。我试图检查不同的答案也在stackoverflow中。但是我仍然无法解决这个问题。

2 个答案:

答案 0 :(得分:2)

可能只是一个错字,但

.babel s 的rc应该为@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_monitoring, container, false); TextView tempNumberTextView = root.findViewById(R.id.tempNumber) return root; }

答案 1 :(得分:0)

您好,我找到了解决方案。实际上,我在命名 .babelrc 文件时有错字,我误将其命名为 .babelsrc