DEV Community

Cover image for No More Broken Image Paths! Meet assets-mapper for React & Next.js πŸš€
Nauman Majeed
Nauman Majeed

Posted on

No More Broken Image Paths! Meet assets-mapper for React & Next.js πŸš€

Have you ever changed an image in your project and suddenly half your UI broke because of that one annoying error?

Module not found: Can't resolve '../assets/logo.png'
Enter fullscreen mode Exit fullscreen mode

Yeah, me too 😩
After facing this frustration again and again, I decided to automate the entire process.


The Problem

In React or Next.js projects, developers often face:

  • Tedious manual imports for every image
  • Long relative paths like (../../../assets/...)
  • Errors when moving or renaming image files
  • Inconsistent paths across components
  • Time wasted fixing broken imports

Every time you reorganize your assets folder, something breaks. It’s messy, repetitive, and completely avoidable.


The Solution β€” assets-mapper πŸš€

assets-mapper automatically scans your image folders and generates a TypeScript-safe asset map β€” so you never have to deal with broken paths again.

Instead of writing:

import logo from '../../assets/images/logo.png';
Enter fullscreen mode Exit fullscreen mode

You can now write:

import { logo } from 'assets-mapper';
Enter fullscreen mode Exit fullscreen mode

That’s it.
No configuration. No path issues. Works beautifully with both React and Next.js.


Why assets-mapper?

βœ… No more broken paths β€” catch missing assets at build time
βœ… TypeScript autocomplete β€” IntelliSense for your images
βœ… Smart duplicate handling β€” adds folder prefixes only when needed
βœ… File watching β€” automatically regenerates when assets change
βœ… Framework agnostic β€” works with React, Next.js, Vue, and more
βœ… Zero configuration β€” works out of the box


Installation

npm install assets-mapper
Enter fullscreen mode Exit fullscreen mode

Quick Start

CLI Usage

# Basic usage
npx assets-mapper --src src/assets --out src/assetsMap.js

# With file watching (recommended for development)
npx assets-mapper --src src/assets --out src/assetsMap.js --watch

# For Next.js public folder
npx assets-mapper --src public/images --out src/assetsMap.js --public
Enter fullscreen mode Exit fullscreen mode

Programmatic Usage

const { generateAssetsMap } = require('assets-mapper');

const result = generateAssetsMap({
  src: 'src/assets',
  out: 'src/assetsMap.js'
});

console.log(`βœ… Generated map with ${result.totalFiles} assets`);
Enter fullscreen mode Exit fullscreen mode

Example

Your folder structure:

src/assets/
β”œβ”€β”€ logo.png
β”œβ”€β”€ hero.jpg
β”œβ”€β”€ icons/
β”‚   β”œβ”€β”€ home.svg
β”‚   └── logo.png
└── images/
    └── banner.webp
Enter fullscreen mode Exit fullscreen mode

Generated assetsMap.js:

import logo from "./assets/logo.png";
import hero from "./assets/hero.jpg";
import home from "./assets/icons/home.svg";
import icons_logo from "./assets/icons/logo.png";
import banner from "./assets/images/banner.webp";

const assetsMap = {
  logo,
  hero,
  home,
  icons_logo,
  banner
};

export default assetsMap;
Enter fullscreen mode Exit fullscreen mode

Use it in your components:

import assetsMap from './assetsMap.js';

function Header() {
  return (
    <header>
      <img src={assetsMap.logo} alt="Logo" />
      <img src={assetsMap.hero} alt="Hero" />
    </header>
  );
}
Enter fullscreen mode Exit fullscreen mode

Smart Features

  • Duplicate Handling: Adds folder prefixes only when filenames conflict
  • File Watching: Automatically regenerates when you add, remove, or rename assets
  • Auto-cleanup: Removes generated files when the package is uninstalled
  • TypeScript support: Full autocomplete and safety in editors

Requirements

  • Node.js 14+
  • Works with React, Next.js, Vue, Svelte, and any JavaScript or TypeScript project

Made for Developers

Built with one simple goal:

Save developers from the pain of broken image paths.

Made with ❀️ for developers who just want things to work smoothly.


Links
npm: https://www.npmjs.com/package/assets-mapper
GitHub: https://github.com/inaumanmajeed/assets-mapper


Top comments (0)