skip to Main Content

I’m facing an issue while running my docker container for a NestJS project since yesterday for unknown reasons, I can build and run start:prod or start:dev without any issue locally, but when it comes to deploying it to prod (running the container on docker), I’m getting this error:

The error :

node:internal/modules/cjs/loader:1080
  throw err;
  ^

Error: Cannot find module 'typescript'
Require stack:
- /usr/src/app/node_modules/nestjs-i18n/dist/utils/typescript.js
- /usr/src/app/node_modules/nestjs-i18n/dist/utils/index.js
- /usr/src/app/node_modules/nestjs-i18n/dist/services/i18n.service.js
- /usr/src/app/node_modules/nestjs-i18n/dist/i18n.module.js
- /usr/src/app/node_modules/nestjs-i18n/dist/index.js
- /usr/src/app/dist/translation/translation.service.js
- /usr/src/app/dist/mylerz/mylerz.service.js
- /usr/src/app/dist/orders/order.service.js
- /usr/src/app/dist/wallet/wallet.service.js
- /usr/src/app/dist/users/user.service.js
- /usr/src/app/dist/notification/notification.service.js
- /usr/src/app/dist/app.service.js
- /usr/src/app/dist/app.controller.js
- /usr/src/app/dist/app.module.js
- /usr/src/app/dist/main.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (/usr/src/app/node_modules/nestjs-i18n/dist/utils/typescript.js:4:12)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/src/app/node_modules/nestjs-i18n/dist/utils/typescript.js',
    '/usr/src/app/node_modules/nestjs-i18n/dist/utils/index.js',
    '/usr/src/app/node_modules/nestjs-i18n/dist/services/i18n.service.js',
    '/usr/src/app/node_modules/nestjs-i18n/dist/i18n.module.js',
    '/usr/src/app/node_modules/nestjs-i18n/dist/index.js',
    '/usr/src/app/dist/translation/translation.service.js',
    '/usr/src/app/dist/mylerz/mylerz.service.js',
    '/usr/src/app/dist/orders/order.service.js',
    '/usr/src/app/dist/wallet/wallet.service.js',
    '/usr/src/app/dist/users/user.service.js',
    '/usr/src/app/dist/notification/notification.service.js',
    '/usr/src/app/dist/app.service.js',
    '/usr/src/app/dist/app.controller.js',
    '/usr/src/app/dist/app.module.js',
    '/usr/src/app/dist/main.js'
  ]
}

Node.js v18.17.1

My Dockerfile:

FROM node:alpine AS development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build

FROM node:alpine AS production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install -omit=dev

COPY . .

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

RUN npm run postinstall

CMD [ "node", "dist/main.js" ]

My package.json :

{
  "name": "backend",
  "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": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "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",
    "postinstall": "patch-package"
  },
  "dependencies": {
    "@nestjs/cache-manager": "^2.1.0",
    "@nestjs/common": "^10.1.2",
    "@nestjs/config": "^3.0.0",
    "@nestjs/core": "^10.1.2",
    "@nestjs/event-emitter": "^2.0.0",
    "@nestjs/jwt": "^10.1.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/mongoose": "^10.0.1",
    "@nestjs/passport": "^10.0.0",
    "@nestjs/platform-express": "^10.1.2",
    "@nestjs/schedule": "^3.0.1",
    "@nestjs/swagger": "^7.1.4",
    "@sentry/minimal": "^6.19.7",
    "@sentry/node": "^7.60.1",
    "@sentry/tracing": "^7.60.1",
    "@types/multer": "^1.4.7",
    "@types/passport-jwt": "^3.0.9",
    "axios": "^1.4.0",
    "bcrypt": "^5.1.0",
    "buffer-to-stream": "^1.0.0",
    "cache-manager": "^5.2.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "date-fns": "^2.30.0",
    "mongoose": "^7.4.1",
    "mongoose-unique-validator": "^4.0.0",
    "nestjs-i18n": "^10.2.6",
    "passport": "^0.6.0",
    "passport-facebook": "^3.0.0",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "passport-strategy": "^1.0.0",
    "patch-package": "^8.0.0",
    "randomstring": "^1.3.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^5.0.1",
    "rxjs": "^7.8.1",
    "slugify": "^1.6.6",
    "uuid": "^9.0.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.1.10",
    "@nestjs/schematics": "^10.0.1",
    "@nestjs/testing": "^10.1.2",
    "@types/buffer-to-stream": "^1.0.0",
    "@types/cron": "^2.4.0",
    "@types/express": "^4.17.17",
    "@types/jest": "29.5.3",
    "@types/node": "^20.4.5",
    "@types/randomstring": "^1.1.8",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.2.0",
    "@typescript-eslint/parser": "^6.2.0",
    "eslint": "^8.46.0",
    "eslint-config-prettier": "^8.9.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "29.6.2",
    "postinstall-postinstall": "^2.1.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "29.1.1",
    "ts-loader": "^9.4.4",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "^5.1.6"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\.spec\.ts$",
    "transform": {
      "^.+\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

I searched about the issue on both Github Issues and SO but none of them provided me a solution about my specific issue.

2

Answers


  1. You don’t have typescript installed inside your docker container, install it during the build by adding RUN npm install -g typescript in the Dockerfile

    Login or Signup to reply.
  2. It appears to be an issue with this package which you can see lists typescript as a devDependency. The thing is, devDependencies are not installed when NODE_ENV is set to production which is why it works for you locally but not in Docker: you probably don’t have your environment set to "production" on your dev machine. And the package clearly imports from the typescript package meaning it should be a regular dependency not a devDependency. You may want to file a github issue if one doesn’t already exist.

    The solution in the meantime is to just install typescript some other way, e.g. by including it your project’s own dependencies.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search