skip to Main Content

For a while now I’m developing services inside a Visual Studio Code devcontainer with NX as a library for working with mono repos. For the database layer I’m using Prisma ORM and use it to run migrations and generate the types to be used.

Lately the development container was giving issues to I did a clean rebuild on the container. After the install, I ran

for target in 'migrate generate-types'; do
    yarn nx run-many --target $target
done

generate-types is defined as the command prisma generate and migrate is prisma generate dev.

This will migrate all projects and generate all the files. Database is being migrated, types are present, so that works.

Everything is installed, all looks fine until I try to start the service. I get this error:

Error: Cannot find module '/workspaces/mono-repo/node_modules/@prisma/client/number'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1171:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1159:15)
    at resolveExports (node:internal/modules/cjs/loader:584:14)
    at Function.Module._findPath (node:internal/modules/cjs/loader:658:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)
    at Function.Module._load (node:internal/modules/cjs/loader:975:27)
    at Function.Module._load (/workspaces/mono-repo/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:18:31)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Array.Object.defineProperty.value (/workspaces/mono-repo/dist/apps/numbering/main.js:266:18)

I found out that yarn nx runs the latest nx version, so I’ve updated the packages.json but the problem doesn’t go away.

The directory /workspaces/mono-repo/node_modules/@prisma/client/number does contain files:

$ ls /workspaces/mono-repo/node_modules/@prisma/client/number
default.d.ts  edge.d.ts  index-browser.js  index.js                                      package.json  schema.prisma  wasm.js
default.js    edge.js    index.d.ts        libquery_engine-debian-openssl-3.0.x.so.node  runtime       wasm.d.ts

I did ran the migrate and generate types command more often before rebuilding the development container and that gave no issues. But suddenly after a Rebuild without cache the problem came up.

This is currently with every service that is using the Prisma ORM.

These are the packages that are loaded with package.json.

"dependencies": {
    "@azure/storage-blob": "^12.17.0",
    "@bull-board/hapi": "^5.9.1",
    "@bull-board/koa": "^5.9.1",
    "@bull-board/ui": "^5.9.1",
    "@golevelup/nestjs-rabbitmq": "^4.0.0",
    "@nestjs/axios": "^3.0.1",
    "@nestjs/bull": "^10.0.1",
    "@nestjs/bullmq": "^10.0.1",
    "@nestjs/cache-manager": "^2.1.1",
    "@nestjs/common": "^10.2.8",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.2.8",
    "@nestjs/microservices": "^10.2.8",
    "@nestjs/passport": "^10.0.2",
    "@nestjs/platform-express": "^10.2.8",
    "@nestjs/schedule": "^3.0.4",
    "@prisma/client": "^5.8.1",
    "axios": "^1.6.2",
    "bull": "^4.12.2",
    "bullmq": "^5.1.3",
    "cache-manager": "^5.2.4",
    "chokidar": "^3.5.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "cron": "2.4.4",
    "date-fns": "^3.2.0",
    "fs-extra": "^11.1.1",
    "global": "^4.4.0",
    "mime-types": "^2.1.35",
    "minio": "^7.1.3",
    "nest-winston": "^1.9.4",
    "nestjs-minio": "^2.5.4",
    "passport": "^0.7.0",
    "passport-http-bearer": "^1.0.1",
    "passport-local": "^1.0.0",
    "prompts": "^2.4.2",
    "reflect-metadata": "^0.2.1",
    "rxjs": "^7.8.0",
    "sharp": "^0.33.2",
    "slugify": "^1.6.6",
    "tsconfig-paths": "^4.2.0",
    "tslib": "^2.6.2",
    "winston": "^3.11.0",
    "winston-daily-rotate-file": "^4.7.1"
  },
  "devDependencies": {
    "@nestjs/schematics": "^10.0.3",
    "@nestjs/testing": "^10.2.8",
    "@nx-tools/nx-prisma": "^5.1.0",
    "@nx/eslint-plugin": "17.3.1",
    "@nx/jest": "17.3.1",
    "@nx/js": "17.3.1",
    "@nx/linter": "17.3.1",
    "@nx/nest": "17.3.1",
    "@nx/node": "17.3.1",
    "@nx/webpack": "17.3.1",
    "@nx/workspace": "17.3.1",
    "@swc-node/register": "~1.6.7",
    "@swc/core": "~1.3.104",
    "@types/fs-extra": "^11.0.4",
    "@types/jest": "^29.5.8",
    "@types/mime-types": "^2.1.4",
    "@types/node": "~20.11.5",
    "@types/passport-http-bearer": "^1.0.40",
    "@types/passport-local": "^1.0.38",
    "@typescript-eslint/eslint-plugin": "^6.10.0",
    "@typescript-eslint/parser": "^6.10.0",
    "eslint": "~8.56.0",
    "eslint-config-prettier": "9.1.0",
    "jest": "^29.7.0",
    "jest-environment-node": "^29.7.0",
    "nx": "^17.3.1",
    "prettier": "^3.2.4",
    "prisma": "^5.8.1",
    "ts-jest": "^29.1.0",
    "ts-node": "10.9.2",
    "typescript": "~5.3.3"
  }

So in packages.json nx is still the previous version. As I said, an update of that didn’t solve the problem.

Why does it say it’s missing the module, even when there are files and what can I do to solve this?

Edit:
I’ve got a workaround now:

  • Added to paths in tsconfig.base.json: "@database/number": ["node_modules/@prisma/client/number"]
  • Changed export { Prisma } from '@prisma/client/number'; to export { Prisma } from '@database/number'; where the errors occurred.

When I do this, the application starts. Why this works and the original doesn’t I don’t know.

2

Answers


  1. Chosen as BEST ANSWER

    Found another workaround, by setting the output to the client repository. So for instance, a schema library with the name prisma-schema-example and a client library with the name prisma-client-example. Then change the output in the schema.prisma file to output = "../../prisma-client-example/generated/client" so it gets generated inside the library.

    After that, include the engine and schema in the assets of the app that's using it, inside the project.json:

    {
      "name": "example-app",
      ...
      "targets": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              {
                "input": "libs/prisma-client-example/generated/client",
                "glob": "*engine*",
                "output": "."
              },
              {
                "input": "libs/prisma-schema-example/prisma",
                "glob": "**",
                "output": "."
              }
            ],
    ...
    

    Now there is no need to use node_modules to in the path alias.

    Don't forget to add generated to .gitignore.


  2. For those of you running into this problem…here’s what worked for me.

    It seems that prisma generate gets confused with the "output = …" path in the schema file and does not generate the required client properly.

    If you have your setup as below, then you may run into the "module not found" and "createEsmNotFoundErr" issue.

    generator client {
      provider = "prisma-client-js"
      output   = "../../../../node_modules/@prisma/client/my-prisma-client"
    }
    

    Instead replace/change as below:

    generator client {
      provider = "prisma-client-js"
      output   = "../../../../node_modules/@prisma/my-schema/client/my-prisma-client"
    }
    

    Ofcourse, you can change my-schema and my-prisma-client to whatever you want.

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