#TS2307: Cannot find module 'nodemailer' or its corresponding type declarations.

1 messages Ā· Page 1 of 1 (latest)

summer karma
#

I get an error when using nodemailer in nest js

package.json

  "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/microservices": "^10.3.10",
    "@nestjs/mongoose": "^10.0.10",
    "@nestjs/passport": "^10.0.3",
    "@nestjs/platform-express": "^10.0.0",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "cookie-parser": "^1.4.6",
    "express": "^4.19.2",
    "joi": "^17.13.3",
    "mongoose": "^8.5.1",
    "nestjs-pino": "^4.1.0",
    "nodemailer": "^6.10.0",
    "passport": "^0.7.0",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "pino-http": "^10.2.0",
    "pino-pretty": "^11.2.2",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "stripe": "^16.5.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/bcryptjs": "^2.4.6",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/nodemailer": "^6.4.17",
    "@types/passport-jwt": "^4.0.1",
    "@types/passport-local": "^1.0.38",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
#

DockerFile

FROM node:alpine AS development

WORKDIR /usr/src/app

COPY package*.json ./
COPY pnpm-lock.yaml ./

RUN npm install -g pnpm

RUN pnpm install

COPY . .

RUN pnpm build

FROM node:alpine AS production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./
COPY pnpm-lock.yaml ./

RUN npm install -g pnpm

RUN pnpm install --prod

COPY --from=development /usr/src/app/dist ./dist

CMD [ "node", "dist/apps/notifications/main" ]

docker-compose.yml

notifications:
    build:
      context: .
      dockerfile: ./apps/notifications/Dockerfile
      target: development
    command: pnpm run start:dev notifications 
    volumes:
      - .:/usr/src/app:delegated
      - /usr/src/app/node_modules
    environment:
      - WATCHPACK_POLLING=true
      - CHOKIDAR_USEPOLLING=true
    env_file:
      - ./apps/notifications/.env

And this is the code that uses the nodemailer

import { Injectable } from '@nestjs/common';
import { NotifyEmailDto } from './dto/notify-email.dto';
import { ConfigService } from '@nestjs/config';
import * as nodemailer from 'nodemailer';

@Injectable()
export class NotificationsService {
  constructor(private readonly configService: ConfigService) {}

  private readonly transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      type: 'OAuth2',
      user: this.configService.get('SMTP_USER'),
      clientId: this.configService.get('GOOGLE_OAUTH_CLIENT_ID'),
      clientSecret: this.configService.get('GOOGLE_OAUTH_CLIENT_SECRET'),
      refreshToken: this.configService.get('GOOGLE_OAUTH_REFRESH_TOKEN'),
    },
  });

  async notifyEmail({ email }: NotifyEmailDto) {
    this.transporter.sendMail({
      from: this.configService.get('SMTP_USER'),
      to: email,
      subject: 'Sleepr Notification',
      text: 'Test text',
    });
  }
}

humble saffron
#

Can you try with

import nodemailer from 'nodemailer';

Instead of

Import * as nodemailer from ā€˜nodemailer’

summer karma
#

i use monorepo

#

i use vsc brother

summer karma
#

what's wrong?

#

solved by comment load node modules in volume then running docker compose up, then uncomment load node modules in volume then running docker compose up

#

i use windows 11

#

i'm confused why the solution it's just like that

#

yes i do

#

thanks for your help

#

i use vs code brother and i read the log

#

yes it says TS2307: Could not find 'nodemailer' module or corresponding type declaration.

#

but I'm confused because I already installed it

#

The error appears in the log not in the problem tab, because the error appears when compiled and running using docker compose.

#

yes i do

summer karma
#

sorry it

#

it's just unit test

#

Thank @sly berry

#

no I'm not writing unit tests for now, I'm taking an online course, the instructor is using a mac, while I'm using Windows, sometimes there are things that need to be adjusted in Windows

#

and the instructor didn't write the unit test

limpid glacierBOT
#

This post has been marked as resolved. āœ…
Please read through the conversation and resolution, if you are having the same issue.
If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.

summer karma
#

Yes, I did.

#

interesting

#

btw, what os are you using?

#

what linux are you using? arch?

summer karma
#

@sly berry