Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type

developerExecutive picture developerExecutive · Mar 19, 2019 · Viewed 7.8k times · Source

I am using styleguidist for my react application, here is the code of my styleguid.config.js file:

module.exports = {
  webpackConfig: {
    module: {
      rules: [
        // Babel loader, will use your project’s .babelrc
        {
          test: /\.jsx?$/,
          exclude: /node_modules/,
          loader: 'babel-loader'
        },
        // Other loaders that are needed for your components
        {
          test: /\.css$/,
          loader: 'style-loader!css-loader?modules'
        }
      ]
    }
  }
}

Now I want to show barcode image in my styleguidist server which is running in http://localhost:6060. Code of my styleguidist readme.md

```jsx
var Macybarcode = require('../../../containers/assets/img/macy/barcode.png');
const barcode_img = Macybarcode;
const barcode_no = "33527111690008";

<BarcodeMacy1 imgString={barcode_img} lineString={barcode_no} paddingTop="20px" width="50%" height="40px" />   
```

But whenever I am trying to show image my server is giving following error:

Please click to see the Error console

Answer

Marty picture Marty · Mar 19, 2019

You need url-loader for images

Use:

npm install url-loader --save

module.exports = {
  webpackConfig: {
    module: {
      rules: [
        // Babel loader, will use your project’s .babelrc
        {
          test: /\.jsx?$/,
          exclude: /node_modules/,
          loader: 'babel-loader'
        },
        // Other loaders that are needed for your components
        {
          test: /\.css$/,
          loader: 'style-loader!css-loader?modules'
        },
        {
                test: /\.(jpg|png|svg)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 25000
                    }
                }
            }
      ]
    }
  }
}