TS2307: Cannot find module 'joi' or its corresponding type declarations

Hi!

Problem appears at the building stage (logs below).

Joi package is installed. Works in dev env and builds for prod fine if runs local. Joi is used for validatoin config file (validationSchema blolow).

I’ve tried to set up tsconfig.json key “esModuleInterop” to true and install @types/joi but the error still appears.

I would be grateful for help.

package.json

{
  "name": "react-shop-back",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "cross-env NODE_ENV=development nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "cross-env NODE_ENV=production node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "10.2.7",
    "@nestjs/config": "3.1.1",
    "@nestjs/core": "10.2.7",
    "@nestjs/jwt": "10.1.1",
    "@nestjs/mapped-types": "2.0.2",
    "@nestjs/mongoose": "10.0.1",
    "@nestjs/passport": "10.0.2",
    "@nestjs/platform-express": "10.2.7",
    "@types/bcrypt": "5.0.0",
    "bcrypt": "5.1.1",
    "class-transformer": "0.5.1",
    "class-validator": "0.14.0",
    "cross-env": "^7.0.3",
    "joi": "^17.11.0",
    "mongoose": "7.6.1",
    "passport": "0.6.0",
    "passport-jwt": "4.0.1",
    "passport-local": "1.0.0",
    "reflect-metadata": "0.1.13",
    "rimraf": "5.0.5",
    "rxjs": "7.8.1",
    "slug": "8.2.3"
  },
  "devDependencies": {
    "@nestjs/cli": "10.1.18",
    "@nestjs/schematics": "10.0.2",
    "@nestjs/testing": "10.2.7",
    "@types/express": "4.17.19",
    "@types/jest": "29.5.5",
    "@types/node": "20.8.4",
    "@types/passport-jwt": "3.0.10",
    "@types/passport-local": "1.0.36",
    "@types/supertest": "2.0.14",
    "@typescript-eslint/eslint-plugin": "6.7.5",
    "@typescript-eslint/parser": "6.7.5",
    "eslint": "8.51.0",
    "eslint-config-prettier": "9.0.0",
    "eslint-plugin-prettier": "5.0.1",
    "jest": "29.7.0",
    "prettier": "3.0.3",
    "source-map-support": "0.5.21",
    "supertest": "6.3.3",
    "ts-jest": "29.1.1",
    "ts-loader": "9.5.0",
    "ts-node": "10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "5.2.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

validationSchema

import * as Joi from 'joi';
import { IConfig } from './config.interface';

export const validationSchema = Joi.object<IConfig>({
  node_env: Joi.string().valid('development', 'production'),
  port: Joi.number().default(5000),
  api: {
    apiUrl: Joi.string().required(),
    globalPrefix: Joi.string().required(),
  },
  jwt: {
    secret: Joi.string().required(),
    expiresIn: Joi.string().required(),
  },
  mongodb: {
    database: {
      connectionString: Joi.string().required(),
      databaseName: Joi.string().required(),
    },
  },
});

Logs

Oct 12 10:55:29 PM  ==> Cloning from https://github.com/alexhcj/react-shop-back...
Oct 12 10:55:33 PM  ==> Checking out commit 6b13828fc331a832b7babc5dce9d097b8beb6934 in branch dev
Oct 12 10:55:35 PM  ==> Downloading cache...
Oct 12 10:55:48 PM  ==> Transferred 114MB in 8s. Extraction took 4s.
Oct 12 10:55:50 PM  ==> Using Node version 18.14.2 via environment variable NODE_VERSION
Oct 12 10:55:50 PM  ==> Docs on specifying a Node version: https://render.com/docs/node-version
Oct 12 10:55:50 PM  ==> Running build command 'npm; npm run build'...
Oct 12 10:55:50 PM  npm <command>
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  Usage:
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  npm install        install all the dependencies in your project
Oct 12 10:55:50 PM  npm install <foo>  add the <foo> dependency to your project
Oct 12 10:55:50 PM  npm test           run this project's tests
Oct 12 10:55:50 PM  npm run <foo>      run the script named <foo>
Oct 12 10:55:50 PM  npm <command> -h   quick help on <command>
Oct 12 10:55:50 PM  npm -l             display usage info for all commands
Oct 12 10:55:50 PM  npm help <term>    search for help on <term>
Oct 12 10:55:50 PM  npm help npm       more involved overview
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  All commands:
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM      access, adduser, audit, bugs, cache, ci, completion,
Oct 12 10:55:50 PM      config, dedupe, deprecate, diff, dist-tag, docs, doctor,
Oct 12 10:55:50 PM      edit, exec, explain, explore, find-dupes, fund, get, help,
Oct 12 10:55:50 PM      hook, init, install, install-ci-test, install-test, link,
Oct 12 10:55:50 PM      ll, login, logout, ls, org, outdated, owner, pack, ping,
Oct 12 10:55:50 PM      pkg, prefix, profile, prune, publish, query, rebuild, repo,
Oct 12 10:55:50 PM      restart, root, run-script, search, set, shrinkwrap, star,
Oct 12 10:55:50 PM      stars, start, stop, team, test, token, uninstall, unpublish,
Oct 12 10:55:50 PM      unstar, update, version, view, whoami
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  Specify configs in the ini-formatted file:
Oct 12 10:55:50 PM      /opt/render/.npmrc
Oct 12 10:55:50 PM  or on the command line via: npm <command> --key=value
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  More configuration info: npm help config
Oct 12 10:55:50 PM  Configuration fields: npm help 7 config
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  npm@9.5.0 /opt/render/project/nodes/node-18.14.2/lib/node_modules/npm
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  > react-shop-back@0.0.1 prebuild
Oct 12 10:55:50 PM  > rimraf dist
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  
Oct 12 10:55:50 PM  > react-shop-back@0.0.1 build
Oct 12 10:55:50 PM  > nest build
Oct 12 10:55:50 PM  
Oct 12 10:55:53 PM  DeprecationWarning: 'getMutableClone' has been deprecated since v4.0.0. Use an appropriate `factory.update...` method instead, use `setCommentRange` or `setSourceMapRange`, and avoid setting `parent`.
Oct 12 10:55:55 PM  src/config/validation.ts:1:22 - error TS2307: Cannot find module 'joi' or its corresponding type declarations.
Oct 12 10:55:55 PM  
Oct 12 10:55:55 PM  1 import * as Joi from 'joi';
Oct 12 10:55:55 PM                         ~~~~~
Oct 12 10:55:55 PM  
Oct 12 10:55:55 PM  Found 1 error(s).
Oct 12 10:55:55 PM  
Oct 12 10:55:55 PM  ==> Build failed 😞

Hi Alex,

It looks like your build command includes npm with no command.

If you revisit the error log you attached, it does include some details about how npm needs to be used. giving that a close read would be the best place to get started.

It never hurts spin up one of our quick starts as a reference to see what a working service uses for all the various settings.

Regards,

Matt

1 Like

Thank you for reply and help!

The problem was in “empty” npm command hence new packages haven’t been installed.
I’ve edited build command to “npm install; npm run build” and all start working.

I wish everyone attentiveness and patience :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.