简体   繁体   English

为什么使用Webpack-dev-server启动React App返回我的目录结构

[英]Why using Webpack-dev-server to start a React App returned my directory structure

I am new to webpack-dev-server and have this question. 我是webpack-dev-server的新手,并有此问题。 Can you please explain and help me to fix it? 您能解释一下并帮助我修复它吗?

First, I am sure my application is running correctly without webpack-dev-server. 首先,我确定我的应用程序在没有webpack-dev-server的情况下可以正常运行。 However, using Webpack-dev-server to start a React App didn't return my hello world page, instead it returned my directory structure... I believe I may miss something here. 但是,使用Webpack-dev-server启动React App并没有返回我的世界世界页面,而是返回了我的目录结构...我相信我在这里可能会错过一些东西。

Without React, I can first run webpack --config webpack.config.js to create my bundle.js. 如果没有React,我可以先运行webpack --config webpack.config.js来创建我的bundle.js。 And then, execute webpack-dev-server --open to open my main page. 然后,执行webpack-dev-server --open打开我的主页。

Now this procedure doesn't work for me when I am trying to start a React app. 现在,当我尝试启动React应用程序时,此过程对我不起作用。 It turns my development directory instead as the screenshot below: 它变成了我的开发目录,而不是下面的截图:

在此处输入图片说明

My hello world app is: 我的世界应用程序是:

在此处输入图片说明

Hello.js: Hello.js:

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

var Hello = React.createClass({
    render: function(){
        var message = "Hello, world!!!!!";
        return (
            <div>
                {message}
            </div>
            );
    }
});


ReactDOM.render(
    <Hello />,
    document.getElementById("root")
);

index.html: index.html的:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World</title>

  </head>
  <body>
    <div id="root"></div>
    <script src= "bundle.js"></script>
    <h2>HEY!</h2>
  </body>
</html>

webpack.config.js: webpack.config.js:

module.exports = {
    entry: "./app/components/Hello.js",
    output: {
        filename: "public/bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: "babel-loader",
                query: {
                    presets: ["es2015", "react"]
                }
        }]
    }

}; };

It's because your index.html is inside /public and not in the root of the website. 这是因为您的index.html位于/ public内部,而不位于网站的根目录中。 If you browse to http://localhost:8080/public you should find it works 如果浏览到http:// localhost:8080 / public ,则应该可以正常使用

Because you don't setup webpack dev server properly. 因为您没有正确设置webpack开发服务器。 Add

devServer: {
    contentBase: "./public"
},

See webpack-dev-server config list https://webpack.github.io/docs/webpack-dev-server.html#api 请参阅webpack-dev-server配置列表https://webpack.github.io/docs/webpack-dev-server.html#api

Also you have to set publicPath on output in your webpack config. 另外,您还必须在webpack配置中的output上设置publicPath Because you called bundle.js in index.html like this <script src= "bundle.js"></script> (no /public folder) 因为您在index.html中像这样<script src= "bundle.js"></script> (没有/public文件夹)调用bundle.js

output: {
    publicPath: '/',
    filename: 'bundle.js'
},

Hope this helps 希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 npm 不会启动反应应用程序,需要依赖项:“webpack-dev-server”:“3.1.14” - npm wont start a react app, requires a dependency: "webpack-dev-server": "3.1.14" 未定义Perf - 使用webpack-dev-server运行react app - Perf is not defined - running react app with webpack-dev-server 尽管将webpack-dev-server的historyApiFallback设置为true,但仍在我的React应用中接收404 - Receiving 404s in my React app despite setting historyApiFallback to true for webpack-dev-server 带有redux和react的webpack-dev-server阻止了我的输入 - webpack-dev-server with redux and react is blocking my inputs webpack-dev-server React 的问题 - Problems with webpack-dev-server React 节点,Webpack-Dev-Server,React的开发模式-嵌套路由没有此类文件或目录 - Dev Mode for Node, Webpack-Dev-Server, React - No Such File or Directory for Nested Routes 将react / node应用程序部署到heroku时出现webpack-dev-server错误 - webpack-dev-server error when deploying react/node app to heroku webpack-dev-server和webpack-&gt;将文件输出到父目录,并使webpack-dev-server热重装仍然有效 - webpack-dev-server and webpack -> output file to parent directory and have webpack-dev-server hot reloading still working Webpack不创建输出文件(不使用webpack-dev-server) - Webpack does not create output file (not using webpack-dev-server) 使用 webpack-dev-server 找不到模块“webpack” - Cannot find module 'webpack' using webpack-dev-server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM