简体   繁体   English

Webpack已成功编译但未在浏览器中显示输出

[英]Webpack successfully compiled but does not show output in browser

The webpack in my project runs successfully but when I reach to the desired port, it shows me the directory files. 我的项目中的webpack成功运行,但是当我到达所需的端口时,它会显示目录文件。 You can look at the image for the same. 你可以看一下图像。

在此输入图像描述

My webpack.config.js 我的webpack.config.js

const path = require('path');

module.exports = {
  entry: './app/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  devServer: {
    inline: true,
    port: 8100
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  }
}

package.json 的package.json

{
  "name": "react_router",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server"
  },
  "author": "vinay",
  "license": "ISC",
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-router": "^2.0.1"
  },
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  }
}

main.js main.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
ReactDOM.render(<App />, document.getElementById('root'));

app.js app.js

import React, {Component} from 'react';
import {Router, Route, Link, IndexRoute, hashHistory, browserHistory} from 'react-router';

const App = () => <h1>Hello World</h1>

export default App

output==> 输出==>

在此输入图像描述

Any help would be appreciated. 任何帮助,将不胜感激。

edited answer: I was able to get it to work: 编辑回答:我能够让它工作:

1) create the actual dist directory under the root folder 2) make your output in your webpack.config.js look like this: 1)在根文件夹下创建实际的dist目录2)使你的webpack.config.js中的输出看起来像这样:

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

it will tell webpack from where to serve your application. 它将告诉webpack从哪里为您的应用程序提供服务。

3) add the following script to your package.json 3)将以下脚本添加到package.json中

    "build": "webpack -p"

it will build the bundle.js file 它将构建bundle.js文件

also, you will need an index.html file in your dist directory that looks like this: 另外,您需要在dist目录中使用index.html文件,如下所示:

<!DOCTYPE html>
  <html lang="en">
    <head>
     <meta charset="UTF-8">  
     <title>Application<title>
   </head>
   <body>
     <div id="app"></div>
     <script src="bundle.js"></script>
   </body>
</html>

then, you run your build script and after that you can run the webpack server and it should work 然后,你运行你的构建脚本,然后你可以运行webpack服务器,它应该工作

更改import语句以import App from './app.js'

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

相关问题 反应项目编译成功但不显示在浏览器上 - react project compiled successfully but dont show on browser 我的终端正在读取 webpack 编译成功,但连接到 react-dom 后浏览器页面为空 - My terminal is reading webpack compiled successfully but browser page is empty after connecting to react-dom ReactJS 代码编译成功,但未在浏览器上打印 - ReactJS code compiled successfully , but not printing on Browser 执行“npm start”命令后,代码为“COMPILED SUCCESSFULLY”并将我重定向到 web 浏览器。 但是没有任何 output - After executing "npm start" command, the code was "COMPILED SUCCESSFULLY" and redirected me to the web browser. But there wasn't any output 即使成功编译后,Webpack也不会在本地主机中加载输出文件 - Webpack not loading output files in localhost even after compiling successfully React 编译成功但未显示组件 - React compiled successfully but not showing components 在 Heroku 上成功部署了我的应用程序,但没有显示其他设备 - Deployed my application successfully on Heroku but does not show other devices Webpack Output 根路径 ('/') Output 在文件系统中的位置在哪里? - Where Does a Webpack Output Path of Root ('/') Output To In The Filesystem? 在NodeJS中执行Webpack编译的包 - Execute webpack compiled bundle in NodeJS 如何评论将由webpack编译的React - How to comment in react that will be compiled by webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM