【问题标题】:Unable to configure webpack.config file无法配置 webpack.config 文件
【发布时间】:2017-07-08 14:46:42
【问题描述】:

每当我运行webpack-dev-server 时,它都会抛出这个错误:

....webpack/hot/only-dev-server ./src Module not found: Error: Can't resolve './src' in 'C:\....

这是 webpack.config.js 文件

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

module.exports = {
    devtool: "inline-source-map",
    entry: [
        "webpack-dev-server/client?http://127.0.0.1:8080/",
        "webpack/hot/only-dev-server",
        "./src"
    ],
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    resolve: {
        modules: ['node_modules', 'src'],
        extensions: ['.js']
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loaders: ['react-hot-loader','babel-core?presets[]=react,presets[]=es2015'],
            }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    ],
};

包.json

{
  "name": "react-todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.23.1",
    "babel-loader": "^6.3.2",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.23.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-0": "^6.22.0",
    "lodash": "^4.17.4",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.3.0"
  },
  "devDependencies": {
    "react-hot-loader": "^1.3.1"
  }
}

我该如何解决?

【问题讨论】:

  • 您的src 文件夹中有index.* 吗? src 文件夹是否存在?尝试将最后一个入口点设为文件而不是文件夹,例如./src./src/index.js
  • 现在显示cant resolve react-hot,更新了我的代码!
  • npm install --save-dev react-hot-loader了吗?
  • 现在我做到了,再次更新代码!错误继续捆绑!现在抛出:babel-core\index.js in not a loader must have normal or pitch function
  • package.json 的内容更新您的问题。你npm install --save-dev babel-core babel-loader babel-preset-es2015了吗?

标签: javascript webpack ecmascript-6 webpack-dev-server webpack-2


【解决方案1】:

试试这个:

将您的包 json 更新为:

{
  "name": "react-todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.4",
    "react": "^15.4.2",
    "react-dom": "^15.4.2"
  },
  "devDependencies": {
    "react-hot-loader": "^1.3.1",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.3.0",
    "babel": "^6.23.0",
    "babel-core": "^6.23.1",
    "babel-loader": "^6.3.2",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.23.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-0": "^6.22.0",
  }
}

然后从项目文件夹的根目录运行npm install

将您的 webpack.config.js 更新为此

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

module.exports = {
    devtool: "inline-source-map",
    entry: [
        "webpack-dev-server/client?http://127.0.0.1:8080/",
        "webpack/hot/only-dev-server",
        "./src/app.js" // or whatever your entry file is
    ],
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    resolve: {
        modules: ['node_modules', 'src'],
        extensions: ['.js']
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            // the loader should be broken up into two objects
            { test: /\.jsx?$/, loaders: ['react-hot', 'jsx?harmony'], include: path.join(__dirname, 'src') }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    ],
};

下载thisserver.js文件并保存到项目文件夹中。

那就试试npm start

希望这行得通。祝你好运。参考here

【讨论】:

  • 又报错了,查看讨论
  • @JaskaranSinghPuri 哦,我忘了,npm install --save-dev express 也是
【解决方案2】:

您似乎使用的是 Windows(错误消息中的 'C:\...')和 webpack expects windows 路径分隔符,例如 \,而不是 /。你可以试试".\src\your_entry_file.js" 代替"./src" 吗?

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 2020-10-15
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2013-12-29
    • 2021-07-23
    相关资源
    最近更新 更多