expressnestjsyarnpkgpnpm

NestJS Application, `Cannot find module "express"` when installing dependencies in production mode with pnpm


The dependencies in my package.json file look like this:

  "dependencies": {
    "@golevelup/nestjs-graphql-request": "0.1.14",
    "@nestjs/axios": "3.0.0",
    "@nestjs/common": "10.0.3",
    "@nestjs/config": "3.0.0",
    "@nestjs/core": "10.0.3",
    "@nestjs/platform-express": "10.0.3",
    "@nestjs/swagger": "7.0.3",
    "axios": "1.4.0",
    "class-transformer": "0.5.1",
    "class-validator": "0.14.0",
    "discord-interactions": "3.4.0",
    "graphql-request": "6.1.0",
    "helmet": "7.0.0",
    "joi": "17.9.2",
    "lodash": "4.17.21",
    "reflect-metadata": "0.1.13",
    "rxjs": "7.8.1",
    "swagger-ui-express": "4.6.3"
  },

If I install all dependencies using pnpm, 8.6.5 and run the app with "start:dev": "nest start --watch", - everything works fine.

However if I:

  1. Install all dependencies (to have nest cli)
  2. Make a build with nest build
  3. Install only production dependencies with pnpm i -P
  4. Run the app with node ./dist/src/main.js

I get an error:

Error: Cannot find module 'express'

If I perform a similar process with yarn, 1.22.19 (steps 1-4), everything works fine

=== Update ===

Tried to change the config for peer dependencies - the problem persists


Solution

  • The question is somewhat answered on NestJS GH

    It is a good idea to prohibit usage of extraneous dependencies with the eslint plugin