React, Webpack: bundle.js is not generated

React, Webpack: bundle.js is not generated

本文关键字:is not generated js bundle Webpack React      更新时间:2023-09-26

Edit:原因是我运行的是webpack-dev-server,当只运行webpack时,它起作用了。

我正在使用React和Webpack。不生成bundle.js。我在SO上发现了这个问题,这也带来了同样的问题,但我似乎已经安装了所需的依赖项。这是我的webpack.config.js:

var webpack = require('webpack');    
module.exports = {
    entry: [
        'webpack-dev-server/client?http://localhost:8080',
        'webpack/hot/only-dev-server',
        './src/index.js'
    ],
    module: {
        loaders: [{
            test: /'.js?$/,
            exclude: /node_modules/,
            loader: 'react-hot!babel'
        }]
    },
    resolve: {
        extensions: ['', '.js']
    },
    output: {
        path: 'dist',
        publicPath: '/',
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: './dist',
        hot: true
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
};

这是我的包.json:

{
  "name": "plump",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo '"Error: no test specified'" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/fiskpatte/plump.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },
  "bugs": {
    "url": "https://github.com/fiskpatte/plump/issues"
  },
  "homepage": "https://github.com/fiskpatte/plump#readme",
  "devDependencies": {
    "babel-core": "^6.8.0",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "history": "^2.1.1",
    "react": "^15.0.1",
    "react-dom": "^15.0.1",
    "react-router": "^2.4.0"
  }
}

知道吗?

编辑。运行webpack:时的输出

                               Asset      Size  Chunks             Chunk Names
                           bundle.js   1.19 MB       0  [emitted]  main
0.63b359d04fe48d6168fa.hot-update.js   27.9 kB       0  [emitted]  main
63b359d04fe48d6168fa.hot-update.json  36 bytes          [emitted]
chunk    {0} bundle.js, 0.63b359d04fe48d6168fa.hot-update.js (main) 1.11 MB [rendered]
  [318] ./src/components/LoginPage.js 8.93 kB {0} [built]
  [322] ./src/components/Lobby.js 12.2 kB {0} [built]
  [323] ./src/components/SignUp.js 5.6 kB {0} [built]
  [324] ./src/pages/Game.js 27.3 kB {0} [built]
     + 321 hidden modules
webpack: bundle is now VALID.

输出路径可能有问题。。您可以尝试替换./dist 的输出路径吗

这是由于package.json的脚本中缺少一些键值对。用以下内容替换您的脚本对象:

"scripts": {
    "start": "npm run build",
    "build": "webpack -p && webpack-dev-server"
  },

"-p"表示专门用于webpack.config.js的生产环境。然后运行此cmd:$ npm start

$ npm start将调用npm run build cmd,后者将调用"webpack-p&&webpack-dev server"