Issue Deploying TypeScript Application to Netlify: "Cannot find module" Error

I’m trying to deploy my TypeScript application to Netlify. The application deploys without any issues locally, but I keep encountering an error during the build stages when attempting to deploy online.

Here’s the error message I’m encountering:

2 | Failed to compile.
./layout.tsx:5:27
Type error: Cannot find module './components/analytics' or its corresponding type declarations.
  3 | import LocalFont from "@next/font/local";
  4 | import { Metadata } from "next";
> 5 | import { Analytics } from "./components/analytics";
    |                           ^
  6 | 
  7 | export const metadata: Metadata = {
  8 | 	title: {
 ELIFECYCLE  Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1

I have confirming that the files are in their correct directories and that everything is in order. Has anyone else encountered a similar problem before, or does anyone have insights into how to resolve it?

My code is written in TypeScript, so I initially thought about adding a .tsx extension to the end of the folder directory I’m trying to access. However, this approach still didn’t solve the issue

I’ve attempted various solutions, including reinstalling TypeScript, removing next build and adding it back deleting and re-installing my node modules and even commenting out the problematic import to see if the application would deploy without it. Surprisingly, even with the import commented out, the same issue persists. It’s noteworthy that the error message consistently points to the exact location, even when the code in question is commented out.

In case you are using Edge Functions, you need to add file extensions to all your imports. If your imported file is importing something, you need to add file extensions for that nested import as well. This is because Edge functions run on Deno and this is the way Deno module imports work.

Try

import { Analytics } from "./components/analytics.ts";

Leaving this here because it gives a similar a similar TypeError: Module not found error and I have wasted hours on this :slight_smile: