1

I am trying to use webpack for importing images. Like this:

import img from '../assets/bmo.png';

But I keep getting the error:

module not found: '../assets/bmo.png'

I have tried all kinds of different paths to the image:

import img from '../assets/bmo.png';
import img from '../../assets/bmo.png';
import img from './assets/bmo.png';
import img from './src/assets/bmo.png';

Imports of other files do work:

import { GameOver } from "./scenes/game-over";

My folder structure:

index.html
-src
  -scenes
      game-over.ts
  -assets
      bmo.png
-dist
      bundle.js

Why can't I use import to import images?

EDIT

Webpack config:

var path = require('path');
var pathToPhaser = path.join(__dirname, '/node_modules/phaser/');
var phaser = path.join(pathToPhaser, 'dist/phaser.js');
    
module.exports = {
  entry: './src/game.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      { test: /\.ts$/, loader: 'ts-loader', exclude: '/node_modules/' },
      { test: /phaser\.js$/, loader: 'expose-loader?Phaser' },
      { test: /\.(png|jpg|gif)$/, use: [{loader: 'file-loader', options: {
          outputPath: 'images',
      } }]}
    ]
  },
  devServer: {
    contentBase: path.resolve(__dirname, './'),
    publicPath: '/dist/',
    host: '127.0.0.1',
    port: 8080,
    open: true
  },
  resolve: {
    extensions: ['.ts', '.js'],
    alias: {
      phaser: phaser
    }
  }
};
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • are you using `file-loader` to pull in images? Can you share your webpack.config? re: https://webpack.js.org/guides/asset-management/#loading-images – Fraze Apr 19 '19 at 15:01
  • Yes, I just added the webpack config – Kokodoko Apr 19 '19 at 20:19
  • Hmm, I'm not seeing any glaring issues. Ref `import img from './assets/bmo.png';` should work for the import. Sorry I'm not more help... – Fraze Apr 22 '19 at 11:49
  • Me neither! It’s not just a harmless ide warning, webpack really won’t compile the project. – Kokodoko Apr 22 '19 at 15:25

0 Answers0