使用ReactJS,Webpack和Babel的Hello World

时间:2018-10-15 11:04:42

标签: reactjs npm webpack babel

我想用React打印简单的Hello World。这是我的应用目录结构: Directory structure

webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        app: './js/app.js'
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'build')
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    },
    stats: {
        colors: true
    },
    devtool: 'source-map'
};

js / app.js

var React = require('react');
var ReactDOM = require('react-dom');

ReactDOM.render(
    <h1>Hello, world!</h1>,
    document.getElementById('root')
  );

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>
    <link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="root"></div>
<script src="build/app.bundle.js"></script>
</body>
</html>

package.json

{
  "name": "es6-tutorial-react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "webpack": "webpack --mode=development"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ccoenraets/es6-tutorial-react.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ccoenraets/es6-tutorial-react/issues"
  },
  "homepage": "https://github.com/ccoenraets/es6-tutorial-react#readme",
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  }
}

我运行npm run webpack时返回错误:

  

无效的配置对象。 Webpack已使用初始化   与API模式不匹配的配置对象。    -configuration.module具有未知的属性“ loaders”。这些属性有效:object {defaultRules ?, exprContextCritical ?,   exprContextRecursive?,exprContextRegExp?,exprContextRequest ?、   noParse ?、规则?,strictExportPresence?,strictThisContextOnImports ?、   unknownContextCritical?,unknownContextRecursive ?、   unknownContextRegExp?,unknownContextRequest?,unsafeCache ?、   wrappedContextCritical?,wrappedContextRecursive ?、   wrapContextRegExp? }->影响普通模块的选项   (NormalModuleFactory)。 npm ERR!代码ELIFECYCLE npm ERR! errno 1下午   呃! es6-tutorial-react@1.0.0 Webpack:webpack --mode=development   npm ERR!退出状态1 npm ERR! npm ERR!失败于   es6-tutorial-react@1.0.0 Webpack脚本。 npm ERR!这可能不是   npm有问题。上面可能还有其他日志记录输出。

     

npm错误!有关此运行的完整日志,请参见:npm ERR!
  C:\ Users \ User \ AppData \ Roaming \ npm-cache_logs \ 2018-10-15T10_57_59_640Z-debug.log

请。帮助谢谢。

3 个答案:

答案 0 :(得分:0)

您正在使用来自webpack v3的代码,但已安装了webpack v4。加载程序在v4中称为rules。而是遵循最新的指南:https://webpack.js.org/guides/getting-started/https://www.valentinog.com/blog/react-webpack-babel/

您还从@babel安装了新的预设,因此请确保您的.babelrc是正确的,并且也不要遵循某些旧的<= v6指南。

答案 1 :(得分:0)

@Dominic已经说过,请使用rules属性,而不要立即使用loaders。如下所示:

...
module: {
   rules: [
     {
       test: /\.js$/,
       exclude: /(node_modules|bower_components)/,
       use: {
         loader: 'babel-loader',
         options: {
           presets: ['@babel/preset-env', '@babel/preset-react']
         }
       }
     }
   ]
 },
...

您需要解决的另一件事是在您的index.html中:

Change this: <script src="build/app.bundle.js"></script> 
To this: <script src="build/bundle.js"></script> 

因为在webpack.config.js中,您将输出文件名指定为bunlde.js,而不是app.bundle.js

希望有帮助!

答案 2 :(得分:0)

降低版本,您的问题将得到解决。

"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",