Maria Jane
Maria Jane

Reputation: 2403

webpack config error of cannot resolve module

When I run webpack --watch I got error of Cannot resolve module 'js/app.js'. And then my app.min.js did not complied when I do npm run dev.

I've created a git repo and this is my 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 }),
  ],
};

Upvotes: 2

Views: 1869

Answers (1)

Diego Zacarias
Diego Zacarias

Reputation: 793

When using context, the entry path should start with a ./ followed by the relative path to the entry file.

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

Another way to resolve this is without using the context key and having an absolute path in the entry key like so:

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

Upvotes: 1

Related Questions