webpack配置错误无法解析模块

时间:2016-10-01 16:04:03

标签: javascript node.js reactjs webpack babeljs

当我运行webpack时 - 我收到了Cannot resolve module 'js/app.js'的错误。当我npm run dev时,我的app.min.js没有遵守。

我创建了一个git repo,这是我的webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: "js/app.js", //what's wrong with this line?
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0']        }
      }
    ]
  },
  output: {
    path: __dirname + "src",
    filename: "app.min.js" //this is not even complied?
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};

1 个答案:

答案 0 :(得分:1)

使用context时,the entry path should start with a ./后跟条目文件的相对路径。

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/app.js",
  ...
}

解决此问题的另一种方法是不使用context密钥并在entry密钥中设置绝对路径,如下所示:

entry: path.join(__dirname, 'src/js/app.js')