Parsing of the module has failed due to the presence of an unexpected character '' while attempting to import a video file

Trying to create a video player in NextJS using TS. I brought in a video file from my src/assets folder and encountered an error.

Error - ./src/assets/video.mp4 Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

After some research, I discovered that the issue lies with webpack. Some solutions on Stack Overflow suggest adding a custom.d.ts file to the project with the following:

declare module "*.mp4" {
  const src: string;
  export default src;
}

This seems to resolve the error when importing video files, but now I'm facing a compile time error. Another solution involves adding loaders to webpack.config.js. However, which webpack.config.js file do I modify? The only one I can find is in my node_modules folder, specifically in boxicon. Altering these settings without much experience could potentially lead to major issues. Is there a safer way to address this problem?

Answer №1

Just a heads up for anyone reading this in the future, there was no need for a webpack config file to resolve an issue with the video src syntax. I encountered the same problem and found the solution after some trial and error.

Here's an example:

// Assuming the file is located under /public
<Video h={"500px"} w={"900px"} link={"/egfile.mp4"}></Video>

Component file Video.tsx

type Props = {
  h: string,
  w: string,
  showControls?: boolean,
  notSupportedMessage?: string,
  link: string,
  filetype: "video/mp4" | ""
}
export const Video = ({
    h,
    w,
    showControls = true,
    notSupportedMessage = "Your browser does not support the video tag.",
    link,
    filetype = "video/mp4",
}: Props) => {
  return (
    <video 
      style={Style.video}
      height={h} 
      width={w} 
      controls={showControls}>
      <source src={link} type={filetype}></source>
      <span>{notSupportedMessage}</span>
    </video>
  );
};

The issue arises when trying to use an import statement similar to what we do for images:

import egfile from "@/public/egfile.mp4"

Instead of that, simply provide the string directly - in Next.js, the root string "/" corresponds to /public.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Monitor the change in values upon pressing the submit button on Angular

I am currently working with an edit form that contains data in the input fields. <ng-form #infoForm="ngForm" novalidate> <div> <label for="firstName">First Name :</label> <input type=" ...

Encountering issues with accessing a variable before its initialization in the getServerSideProps function in

Currently, I am working on setting up Firebase and configuring the APIs and functions to retrieve necessary data in my firebase.tsx file. Afterwards, I import them into my pages/index.tsx file but I am encountering an issue where I cannot access exports af ...

Navigating the intricacies of debugging sub-domains in Angular projects using Visual Studio Code (VS

Currently working on a massive project utilizing micro-services. The unique design for clients/tenants requires visiting their specific subdomain to select a particular tenant. For example, https://ClientA.localhost:4200 and https://ClientB.localhost:4200. ...

The 'roleName' property is not found within the 'never' data type

// ** React Component and Library Imports import { useEffect, useState } from 'react' import Box, { BoxProps } from '@mui/material/Box' import Button from '@mui/material/Button' import Drawer from '@mui/material/Drawer&ap ...

Converting strict primitive types to primitive types in Typescript

I have a function that parses a string into a value and returns a default value if it fails. The issue is that this code returns too strict types for primitives, such as `false` instead of `boolean`. How can I resolve this? Should I utilize some form of ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

The Angular 2 Router's navigation functionality seems to be malfunctioning within a service

Currently, I am facing an issue with using Angular2 Router.navigate as it is not functioning as expected. import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { Router } from '@angular/ ...

Issue encountered when attempting to access disk JSON data: 404 error code detected

I am attempting to retrieve JSON data from the disk using a service: import { Product } from './../models/Product'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { HttpClient } from &apo ...

The background image fails to load in ReactJS application

Usually, I have no trouble finding solutions to my problems, but this time I'm really stuck - and I apologize in advance if my question seems silly or trivial. I am currently working on a simple React app where I need to set a background image for th ...

Unexpected INTERNAL error encountered with Firebase's Cloud Function update

Currently, I am immersed in a project involving Vue.js 3, Typescript, and Firebase. However, while attempting to integrate new cloud functions, I came across an unexpected issue: Failed to load resource: the server responded with a status of 500 () Un ...

What is the process for retrieving information from a JSON document?

When working on my project using Next.js, I encountered an issue with fetching and displaying data from a JSON file named mainMenu. Could someone please guide me on how to correctly implement this? I have shared my code below: function MyApp({ Component, ...

Creating a module within a component in angular - step by step guide

I am interested in dynamically creating a component inside another component. This will allow me to pass my dynamic HTML template directly to the decorator like this: //code /** * @param template is the HTML template * @param container is @ViewChild(& ...

Is there a syntax available for type annotation of arrays created from pre-declared variables?

According to my standards, all possible annotations are required in TypeScript. Therefore, TypeScript-ESLint is prompting me to annotate the `[ responseData, cognitoUser ]`. However, when I try to use the syntax `[ responseData1: ResponseData1, responseD ...

Encountered difficulty retrieving properties from the req.query object within expressjs

My setup includes a router configuration like this: router.get('/top-5-cheap', aliasTopTours, getAllTours); I've also implemented some middlewares: Middleware aliasTopTours: In this middleware, I am setting certain properties on the reque ...

When trying to implement appDir and withPWA in next.config.js, an error has been encountered

My next.config.js is set up with next-pwa and an experimental app feature included. const withPWA = require('next-pwa'); module.exports = withPWA({ pwa: { dest: 'public', disable: process.env.NODE_ENV === 'development&ap ...

Error message in Angular2 for production build with webpack: "Unable to load app/app.component.html"

My current project is utilizing Angular2-webpack-starter, running on Angular2 rc.4 and webpack 1.13.1. Everything functions smoothly in dev mode. However, when attempting to switch to production mode, I encounter the error message Failed to load app/app.c ...

Firebase is displaying an error when using onSnapshot, specifically when a value is being waited on from an

Currently, I am working on an application where I am categorizing posts and users based on a specific room ID. I am adding a roomid property to both the user and every post. However, when attempting to fetch all posts belonging to a particular room ID, I e ...

Minimize the quantity of data points displayed along the X-axis in a highcharts graph

After making an API call, I received data for a Highcharts graph consisting of an array of datetimes (in milliseconds) and corresponding values (yAxis). The data is fetched every 15 minutes and displayed on a mobile device. When viewing the data monthly, ...

Utilizing the same NextJs page layout, showcase varying sets of information

As I work on my Next.js app, I am faced with the challenge of creating two pages that share the same design but present different data. What is the most effective way to achieve this while ensuring that each page has a unique URL path? ...

What is a way to merge all the letters from every console.log result together?

I'm encountering an issue - I've been attempting to retrieve a string that provides a link to the current user's profile picture. However, when I use console.log(), the output appears as follows: Console Output: Below is my TypeScript code ...