简体   繁体   English

当 react 应用程序未在 localhost:3000 上加载时,如何设置 webpack 和 babel

[英]How do I set up webpack and babel when react app is not loading on localhost:3000

I've followed several tutorials and none seem to work.我已经遵循了几个教程,但似乎都没有用。 I can configure webpack, babel, and some simple React components but they are not rendering on my localhost:3000.我可以配置 webpack、babel 和一些简单的 React 组件,但它们不会在我的 localhost:3000 上呈现。 Any suggestions as to what I am doing wrong as there are no console logs or errors being thrown in terminal.关于我做错了什么的任何建议,因为终端中没有控制台日志或错误。

package.json package.json

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "@babel/core": "^7.11.0",
    "@babel/preset-env": "^7.11.0",
    "@babel/preset-react": "^7.10.4",
    "babel-loader": "^8.1.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-hot-loader": "^4.12.21",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  }
}

.babelrc .babelrc

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

webpack.config.js webpack.config.js

const webpack = require('webpack');
const path = require('path');


module.exports = {
  devtool: 'inline-source-map',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist',),
    publicPath: '/',
    filename: 'bundle.js'
  },
  devServer: {
    port: 3000,
    contentBase: './dist',
    hot: true
  },
  module: {
    rules: [
    {
      test: /\.(js|jsx)$/,
      exclude: /node_modules/,
      use: ['babel-loader']
    }
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ],
};

index.html index.html

<!DOCTYPE html>
<html>
 <head>
 <title>My React Configuration Setup</title>
 </head>
 <body>
 <div id="root"></div>
 <!-- <script src="./dist/bundle.js"></script> -->
 </body>
</html>

index.js索引.js

import React from "react";
import ReactDOM from "react-dom";
import Welcome from './App'

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

module.hot.accept();

App.js应用程序.js

import React from 'react';

class Welcome extends React.Component {
  render() {
    return <h1>Hello World from React boilerplate</h1>;
  }
}

export default Welcome

You are almost there.你快到了。

  1. build bundle.js using webpack command from package.json with below使用来自package.jsonwebpack命令构建bundle.js ,如下所示
  "scripts": {
    "start": "webpack && webpack-dev-server --mode development",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

or要么

  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server --mode development",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  1. Modify webpack.config.js contentBase property since your index.html leaves at root (you could move it to dist )修改webpack.config.js contentBase属性,因为您的index.html位于根目录(您可以将其移动到dist
  devServer: {
    port: 3000,
    contentBase: './',
    hot: true
  },
  1. Uncomment bundle.js import from index.html :取消注释bundle.jsindex.html导入:
 <div id="root"></div>
    <script src="./dist/bundle.js"></script>
 </body>

You could use create-react-app to initialize an empty React project really fast你可以使用create-react-app来非常快速地初始化一个空的 React 项目

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

相关问题 如何在反应中将 localhost:3000 更改为 custom.domain - How do I change localhost:3000 to custom.domain in react 使用 babel 和 webpack 时如何生成 sourcemap? - How do I generate sourcemaps when using babel and webpack? 如何设置类似 Instagram 的路线? 本地主机:3000/用户名 - How can I set a route like Instagram? localhost:3000/USERNAME React:应用程序在 localhost:3000/some/path 上运行 - React: App runs on localhost:3000/some/path Webpack + Babel 正在转译 Webpack 加载器,我该如何防止? - Webpack + Babel is transpiling Webpack loaders, how do I prevent this? 我需要在 2021 年在 React 项目中手动设置 babel 和 webpack - do I need setup babel and webpack manually in 2021 in react project 无法使用Webpack,Babel加载React和ReactDOM - React and ReactDOM not loading using webpack, babel 如何使用babel和webpack设置ua react / node项目? - how to set u a react/node project with babel and webpack? 当我在 react app 上执行 npm start 时,不断收到“?端口 3000 上已经有东西在运行” - keep getting "? Something is already running on port 3000" when I do npm start on react app React 应用程序在 localhost:3000 上显示空白页面,但在 localhost:3001 上工作 - React app is showing blank page on localhost:3000 but working on localhost:3001
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM