C.Ronaldo
C.Ronaldo

Reputation: 609

React img shows [object Module] instead of the image

Recently I pushed my project to Gitlab so I could also work on it on my laptop to practise my React. After I pulled and npm installed everything (had to use npm audit fix --force which was weird) I tried to run it. Everything looked fine but the images wont show up and when I inspect it, it says [object Module]. Now I have googled it and most answers say something about changing the options to

options: {
      esModule: false,
    },

in webpack

The thing is, I think I never worked with webpack and on my desktop it works fine (I don't have a webpack.config.js). This is my package.json which is the same as on my laptop:

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "bootstrap": "^4.5.0",
    "cors": "^2.8.5",
    "node-sass": "^4.14.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1",
    "react-select": "^3.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {}
}

Upvotes: 3

Views: 2627

Answers (2)

Shibu
Shibu

Reputation: 510

To Change this Go to node_modules/react-scripts/config/webpack.config.js

{
          test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
          loader: require.resolve('url-loader'),
          options: {
            limit: imageInlineSizeLimit,
            name: 'static/media/[name].[hash:8].[ext]',
            esModule: false
          },
        }

Find the above line and add esModule: false in the options.

Afterward, the image will display.

Upvotes: 0

David Gaspar
David Gaspar

Reputation: 463

I used the 'default' property to access the string value (path):

<img 
   src={require("../../assets/images/svg/shopping-cart.svg").default}
   alt="Shopping cart"/>

This solution I took from Carlos Avila's comment to a vuejs GitHub issue.

Upvotes: 6

Related Questions