React不在Webpack Dev服务器上呈现

时间:2016-12-06 01:27:57

标签: reactjs webpack

我有这个工作,不记得我改变了什么,现在页面加载但没有反应呈现在屏幕上。不确定错误在哪里。运行webpack -m后跟npm start

时不会发生错误

webapp文件夹就像这样...

  • {应用} - App.js
  • {公共} - bundle.js - index.html
  • webpack.config.js
  • .babelrc
  • 的package.json

这是重要的文件

的WebPack

module.exports = {
  entry: './app/App.js',
  output: {
    path: './public',
    filename: 'bundle.js',
  },
  devServer: {
      inline:true,
      contentBase: './public',
      port: 3333
    },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      }
    ]
  }
}

的package.json

{
  "name": "newaccount",
  "version": "1.0.0",
  "description": "a form submission for new accounts",
  "main": "App.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "---",
  "license": "ISC",
  "dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-router": "^3.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "html-webpack-plugin": "^2.24.1",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2"
  }
}

.babelrc

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

App.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';



class Testing extends Component {
    render() {
        return (
            <div>
                <h3>JUST A TEST</h3>
            </div>
        )
    }
}

ReactDOM.render(<Testing />, document.getElementById('app'));

我曾尝试将组件测试与ES6 const一样......

const Testing = () => return <div><h3>JUST A TEST</h3></div>

并且仍然无法在localhost:3333或我计算机的完整路径上显示任何内容。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我能够通过做三件事来让你的榜样成功。

首先,在HTMLWebpackConfiguration的顶部添加webpack.config.js。然后,在index.html中添加/app页面。最后,确保配置指向HTML文件。最终webpack.config.js看起来像这样:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: __dirname + '/app/App.js',
  output: {
    path: __dirname + '/public',
    filename: 'bundle.js',
  },
  plugins: [HTMLWebpackPluginConfig],
  devServer: {
      inline:true,
      contentBase: './public',
      port: 3333
    },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      }
    ]
  }
}

app/index.html看起来像这样:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My App</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div id="app"></div>
</html>

信息来源:

https://tylermcginnis.com/react-js-tutorial-1-5-utilizing-webpack-and-babel-to-build-a-react-js-app-5f804d729d3b#.f4sslv7ub

Invariant Violation: _registerComponent(...): Target container is not a DOM element

http://javascriptplayground.com/blog/2016/07/webpack-html-plugin/

答案 1 :(得分:2)

我遇到了同样的问题。您可能想重新访问此解决方案。

检查webpack-dev-server文档后,我意识到我需要在webpack config中指定devServer属性集合。您已经在我的回复前7个月的示例中完成了此操作。他们的文档示例:

devServer: {
  contentBase: path.join(__dirname, "dist"),
  compress: true,
  port: 9000
}

这解决了我甚至没有加载index.html的情况(根据浏览器网络调试列表)。

我无需在任何地方添加HTMLWebpackConfiguration

https://webpack.js.org/configuration/dev-server/