webpack和webpack-dev-server版本兼容,亲测有效

这两天准备亲手搭建一个react项目,本以为会很顺利,没想到遇到了很多兼容性的问题。刚开始遇到webpack和babel的版本不兼容,之后使用webpack-dev-server搭建服务器遇到安装的webpack和webpack-dev-server版本不兼容的问题。作为一名前端小白的我表示心很累。

现象:安装webpack之后运行正常,再安装不同版本的webpack-dev-server,出现了许多不同的错误,以下列举两种错误。
webpack和webpack-dev-server版本兼容,亲测有效_第1张图片
webpack和webpack-dev-server版本兼容,亲测有效_第2张图片

解决方法:嗯。。。因是版本不兼容的问题,解决方法还是改变webpack和webpack-dev-server安装版本,还有babel的版本。。。
webpack.config.js

const path = require('path')

module.exports = {
    entry: path.join(__dirname, 'src/index.js'),
    output: {
        path: path.join(__dirname, './dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            use: ['babel-loader?cacheDirectory=true'],
            exclude: path.resolve(__dirname, 'node_modules')
        }]
    },
    devServer: {
        port: 8080,
        contentBase: path.join(__dirname, './dist'),
        historyApiFallback: true,
        host: '127.0.0.1'
    }
}

package.json文件

{
  "name": "react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --config webpack.dev.config.js",
    "start": "webpack-dev-server --config webpack.dev.config.js --color --progress"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.9.7"
  },
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-router-dom": "^4.3.1"
  }
}

[email protected][email protected]也是可以的,亲测有效

你可能感兴趣的:(webpack)