"Cannot find module" error when importing mp3

I'm using useSound within a NextJS app, and I would like to play a mp3 located in /public/static:

import notification from "../../public/static/notification.mp3"

It is working when running yarn dev (sound is playing, no errors), but it raises an error when building with yarn prod:

Type error: Cannot find module '../../public/static/notification.mp3' or its corresponding type declarations.

Could someone help me with that? I've been looking over the internet/SO/github, and tried several things, including modifying next.config.js:

  webpack(config, { isServer }) {
    config.module.rules.push({
      test: /\.(ogg|mp3|wav|mpe?g)$/i,
      exclude: config.exclude,
      use: [
        {
          loader: require.resolve('url-loader'),
          options: {
            limit: config.inlineImageLimit,
            fallback: require.resolve('file-loader'),
            publicPath: `${config.assetPrefix}/_next/static/`,
            outputPath: `${isServer ? '../' : ''}static/`,
            name: '[name]-[hash].[ext]',
            esModule: config.esModule || false,
          },
        },
      ],
    });
    return config;
  },

Without success..



Read more here: https://stackoverflow.com/questions/67003140/cannot-find-module-error-when-importing-mp3

Content Attribution

This content was originally published by Binajmen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: