How do I load images with loadImage() and webpack?

Hello, I am using p5 in instance mode with an Express server in Node.js. I am importing the p5.js library via npm and webpack rather than with an inline script tag on an html document. This has been working fine, but I’ve hit a wall now that I’m trying to import images.
I worked through the Asset Management guide on, and successfully loaded images via .css document, indicating that webpack and the file-loader module are configured correctly. However, when trying to load an image with p5 using loadImage(), webpack sends the following error upon building the bundle:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.

Here’s how everything is configured (please excuse the silly image file names):


const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
    rules: [
        test: /\.css$/,
        use: [
        test: /\.(png|svg|jpg|gif)$/,
        use: [

index.js (see line 14, which causes the error):

import p5 from 'p5';
import Planet from './modules/Planet.js';
import './style.css';
import './assets/zuckervoid.jpg';

const containerElement = document.getElementById('p5-container');

const sketch = (p) => {
  let np = 5;

  let planets = [];
  let planet_texture;
  p.preload = function(){
    planet_texture = p.loadImage('./assets/zuckervoid.jpg',,(e) => {
      //if image fails...

  p.setup = function() {
    p.createCanvas(p.windowWidth, p.windowHeight, p.WEBGL);
    for(let i = 0; i < np; i++){
      let x = Math.floor(Math.random()*p.width);
      let y = Math.floor(Math.random()*p.height);
      planets.push(new Planet(p,x,y));

  p.draw = function() {
    for(let i = 0; i < np; i++){

new p5(sketch, containerElement);


  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="shortcut icon" href="£"></link>
  <div id="p5-container"></div>
  <script src="main.js">

Sorry in advance, I’m not familiar with Webpack, but I did find this, maybe it can start you off?

1 Like