4

I have two folders in root directory of project - build and src. I would like to move images from src folder to build folder. However i'm getting error "Cannot resolve 'file'" despite i've included the url-loader plugin.

header.jsx

import {React, ReactDOM} from '../../build/react';

export default class Header extends React.Component {

  render() {
    return (
        <div className="header">
        <img className="logo" src={require("../src/content/images/program-brand-logo.jpg")} />
        </div>
      );
    }
  };

Error log:

ERROR in ./src/components/header.jsx
Module not found: Error: Cannot resolve 'file' or 'directory' ../src/content/images/program-brand-logo.jpg in /Users/username/Desktop/Projects/Demo/Scorecard/SPA/React (Working Copy)/src/components
 @ ./src/components/header.jsx 45:68-123

Webpack.config.js

var webpack = require('webpack');
var path = require('path');
require("babel-polyfill");

var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'src');

module.exports = {
  entry: [
    // Set up an ES6-ish environment
    'babel-polyfill',
    'bootstrap-loader',
    'webpack/hot/dev-server',
    APP_DIR + '/import.js',
  ],
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loader: 'babel',

      exclude: /node_modules/,
      query: {
        plugins: ['transform-runtime'],
        presets: ['es2015', 'stage-0', 'react']
      }
    }, {
      test: /\.css$/,
      loader: "style-loader!css-loader"
    }, {
      test: /\.scss$/,
      loaders: ["style", "css", "sass"]
    }, {
      test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
      loader: 'url-loader?limit=100000'
    }]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
    new webpack.HotModuleReplacementPlugin()
  ]
};
Rahul Dagli
  • 4,301
  • 15
  • 47
  • 85

3 Answers3

4

you don't have to use require for loading images you can import them as other dependecies with import.

import img from './../src/content/images/program-brand-logo.jpg'

.....

<img className="logo" src={img} />

or I thing you should specify your path as folow './../src/content/images/program-brand-logo.jpg'

Josef Prochazka
  • 1,273
  • 2
  • 9
  • 28
  • Still getting same error message: `ERROR in ./src/components/header.jsx Module not found: Error: Cannot resolve 'file' or 'directory' ./../src/content/images/program-brand-logo.jpg in /Users/username/Desktop/Projects/Demo/Scorecard/SPA/React (Working Copy)/src/components @ ./src/components/header.jsx 29:24-81` – Rahul Dagli Feb 05 '16 at 10:39
0

Make sure this path ../src/content/images/program-brand-logo.jpg is relative to header.jsx file

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Mukesh Kumar
  • 944
  • 2
  • 10
  • 26
0

just remove

?limit=100000 from 
test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
      loader: 'url-loader?limit=100000'

and build the webpack your final test loader should look like this:

test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
      loader: 'url-loader'
Rarblack
  • 4,559
  • 4
  • 22
  • 33