Zichzheng
Zichzheng

Reputation: 1290

NestJS Error: Error:Cannot find module './'

I encountered a error when using NestJS. The console shows 0 error first and then crahsed with the Error: Cannot find module './'. This is similar to the

Error:Cannot find module '../module_name'.

However, for this one, it shows './'. And I tried to delete the node_module and the dist folder and rerun the npm install that was usually used to solve the similar people.

The full error message is:

[6:32:11 PM] Starting compilation in watch mode...
[6:32:14 PM] Found 0 errors. Watching for file changes.
Error: Cannot find module './'
Require stack:
- C:\Users\Vibrant\Desktop\inventory_demo\dist\client\client.service.js
- C:\Users\Vibrant\Desktop\inventory_demo\dist\client\client.controller.js
- C:\Users\Vibrant\Desktop\inventory_demo\dist\client\client.module.js
- C:\Users\Vibrant\Desktop\inventory_demo\dist\app.module.js
- C:\Users\Vibrant\Desktop\inventory_demo\dist\main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\Vibrant\Desktop\inventory_demo\src\client\client.service.ts:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

The client.service.js:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("./");
let ClientService = class ClientService {
    async client(clientWhereUniqueInput) {
        return client_1.default.client.findUnique({
            where: clientWhereUniqueInput,
        });
    }
    async clients(params) {
        const { skip, take, cursor, where } = params;
        return client_1.default.client.findMany({
            skip,
            take,
            cursor,
            where,
        });
    }
    async createClient(data) {
        return client_1.default.client.create({ data });
    }
    async deleteClient(where) {
        return client_1.default.client.delete({ where });
    }
    async getClientByID(id) {
        return await client_1.default.client.findUnique({
            where: { client_id: Number(id) },
        });
    }
    async updateClient(params) {
        const { where, data } = params;
        return client_1.default.client.update({ where, data });
    }
};
ClientService = __decorate([
    (0, common_1.Injectable)()
], ClientService);
exports.ClientService = ClientService;
//# sourceMappingURL=client.service.js.map

I have two ts files has client in the name:

First one is called client.ts which is a prism database client:

import { PrismaClient } from '@prisma/client';

interface CustomNodeJsGlobal extends NodeJS.Global {
  prisma: PrismaClient;
}

// Prevent multiple instances of Prisma Client in development
declare const global: CustomNodeJsGlobal;

const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV === 'development') global.prisma = prisma;

export default prisma;

The second one is a model called client, it only imports the basic nestjs modules I think.

The client module.ts

import { Module } from '@nestjs/common';
import { ClientController } from './client.controller';
import { ClientService } from './client.service';

@Module({
  controllers: [ClientController],
  providers: [ClientService]
})
export class ClientModule {}

The client controller(haven't start it yet):

import { Controller } from '@nestjs/common';

@Controller('client')
export class ClientController {}

and the client service:

import { Injectable } from '@nestjs/common';
import { Client, Prisma } from '@prisma/client';
import prisma from 'src/client';

@Injectable()
export class ClientService {
  // The service to return a single client
  async client(
    clientWhereUniqueInput: Prisma.ClientWhereUniqueInput,
  ): Promise<Client> {
    return prisma.client.findUnique({
      where: clientWhereUniqueInput,
    });
  }

  // The service to return a list of clients
  async clients(params: {
    skip?: number;
    take?: number;
    cursor?: Prisma.ClientWhereUniqueInput;
    where?: Prisma.ClientWhereInput;
  }): Promise<Client[]> {
    const { skip, take, cursor, where } = params;
    return prisma.client.findMany({
      skip,
      take,
      cursor,
      where,
    });
  }

  // The service to create a client
  async createClient(data: Prisma.ClientCreateInput): Promise<Client> {
    return prisma.client.create({ data });
  }

  // The service to delete a client
  async deleteClient(where: Prisma.ClientWhereUniqueInput): Promise<Client> {
    return prisma.client.delete({ where });
  }

  // The service to find an client by id
  async getClientByID(id: string) {
    return await prisma.client.findUnique({
      where: { client_id: Number(id) },
    });
  }

  // The service to update a client
  async updateClient(params: {
    where: Prisma.ClientWhereUniqueInput;
    data: Prisma.ClientUpdateInput;
  }): Promise<Client> {
    const { where, data } = params;
    return prisma.client.update({ where, data });
  }

  // End of class
}

Upvotes: 0

Views: 2273

Answers (1)

Zichzheng
Zichzheng

Reputation: 1290

The problem is I used a model called client. The solution is to rename it to clients since NestJS itself uses the name client. After the name modification, I will need to delete the whole dist folder and run npm rebuild.

Upvotes: 1

Related Questions