webpack Module not found issue

I’m new in Webpack (before I used Gulp) and now I want to move the existing application from Gulp to Webpack.

But I’m getting issues like Module not found: Error: Can’t resolve ‘/src/app/index.js’ in ‘/var/www/project’ or Module not found: Error: Can't resolve '/src/styles/main.scss' in '/var/www/project' and same for every file i’m using on my enty chain.

Here is my file structures:

  • package.json
  • .env
  • .env.example
  • webpack.conf.js
  • src/
    • app/
      • index.js
      • other js/vue related files
    • styles/
      • main.scss and all related styles
    • public/
      • index.html
      • favicon.ico
      • images/
        • all images

and here is mine webpack.conf.js:

const HtmlWebpackPlugin = require('html-webpack-plugin'); 
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const Dotenv = require('dotenv-webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');

const webpack = require('webpack');
const ENV = process.env.APP_ENV;
const isDev = ENV === 'dev'
const isProd = ENV === 'prod';
function setDevTool() {
  if (isDev) {
    return 'inline-source-map';
  } else if (isProd) {
    return 'source-map';
  } else {
    return 'eval-source-map';
  }
}
const config = {
  entry: {
    'bundle.min.css': [
      path.resolve(__dirname, '/src/styles/main.scss'),
    ],
    'bundle.js': [
      path.resolve(__dirname, '/src/app/index.js')
    ]
  },
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist'),
  },
  devtool: setDevTool(),
  module: {
    rules: [
      // fonts loader
      {
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath: 'fonts/'
        }
      },
      // babel loader
      {
        test: /.js$/,
        use: 'babel-loader',
        exclude: [
          /node_modules/
        ]
      },
      // raw html loader
      {
        test: /.html/,
        loader: 'raw-loader'
      },
      // css loader
      {
        test: /.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader'
        })
      },
      // sass loader
      {
        test: /.(sass|scss)$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['style-loader', 'css-loader', 'sass-loader']
        })
      },
      // vue loader
      {
        test: /.vue$/,
        loader: 'vue-loader'
      },
      // image url inliner
      {
        test: /.(jpe?g|png|gif)$/,
        loader: 'url-loader',
        options: {
          limit: 50 * 1024
        }
      },
      // svg url inliner
      {
        test: /.svg$/,
        loader: 'svg-url-loader',
        options: {

          limit: 50 * 1024,
          noquotes: true,
        }
      },
      // image loader
      {
        test: /.(jpg|png|gif|svg)$/,
        loader: 'image-webpack-loader',
        enforce: 'pre'
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: __dirname + "/src/public/index.html",
      inject: 'body'
    }),
    new Dotenv({safe: true}),
    new ExtractTextPlugin("bundle.min.css"),
    new VueLoaderPlugin()
  ],
  devServer: {
    contentBase: './src',
    port: 7700,
  }
}

if (isProd) {
  config.plugins.push(
    new UglifyJSPlugin(),
    new CopyWebpackPlugin([{
      from: __dirname + '/src/public'
    }])
  );
};
module.exports = config;                                        

I do not understand why it can’t find those files if for examlpe ‘/var/www/project//src/styles/main.scss’ it mention in error is correct path?

Answer

Try to remove the initial slashes from path.resolve(). For example:

path.resolve(__dirname, '/src/styles/main.scss')

should be:

path.resolve(__dirname, 'src/styles/main.scss')