nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.53k stars 2.35k forks source link

Module not found Error for lib when running built express app #18941

Closed messerbill closed 1 year ago

messerbill commented 1 year ago

Current Behavior

I've built an express app using npx nx g @nx/express:app user-service and two libs using npx nx g @nx/js:lib shared-structures and npx nx g @nx/js:lib services while services has a dependency to shared-structures like so:

project.json of user-service:

{
  "name": "user-service",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/user-service/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/user-service",
        "main": "apps/user-service/src/main.ts",
        "tsConfig": "apps/user-service/tsconfig.app.json",
        "assets": [],
        "isolatedConfig": true,
        "webpackConfig": "apps/user-service/webpack.config.js"
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "user-service:build"
      },
      "configurations": {
        "development": {
          "buildTarget": "user-service:build:development"
        },
        "production": {
          "buildTarget": "user-service:build:production"
        }
      }
    },
    "lint": {
      "executor": "@nx/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["apps/user-service/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nx/jest:jest",
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
      "options": {
        "jestConfig": "apps/user-service/jest.config.ts",
        "passWithNoTests": true
      },
      "configurations": {
        "ci": {
          "ci": true,
          "codeCoverage": true
        }
      }
    }
  },
  "tags": []
}

package-json of user-service:

{
  "name": "user-service",
  "version": "1.6.4",
  "description": "",
  "scripts": {
    "seed:dev": "sequelize-cli db:seed:all --seeders-path seeders/development",
    "seed:prod": "sequelize-cli db:seed:all --env production --seeders-path seeders/production",
    "seed:dev:undo": "sequelize-cli db:seed:undo:all --seeders-path seeders/development",
    "seed:prod:undo": "sequelize-cli db:seed:undo:all --env production --seeders-path seeders/production"
  },
  "pre-commit": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.1.4",
    "jsonwebtoken": "^8.5.1",
    "mysql2": "^2.2.5",
    "reflect-metadata": "^0.1.13",
    "typescript-logging": "^0.6.2",
    "zod": "^3.22.2",
    "services": "*",
    "middlewares": "*"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/cors": "^2.8.10",
    "@types/jsonwebtoken": "^8.5.2",
    "umzug": "^3.2.1"
  }
}

package.json of services:

{
  "name": "services",
  "version": "0.0.1",
  "peerDependencies": {
    "@types/express": "^4.17.15",
    "@types/node": "^18.11.17",
    "@types/jsonwebtoken": "^8.5.2",
    "express": "^4.18.2",
    "jsonwebtoken": "^8.5.1",
    "winston": "^3.8.2"
  },
  "dependencies": {
    "shared-structures": "*"
  },
  "type": "commonjs",
  "main": "./index.cjs",
  "module": "./index.js"
}

project.json of services:

{
  "name": "services",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "libs/services/src",
  "projectType": "library",
  "targets": {
    "build": {
      "executor": "@nx/rollup:rollup",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/libs/services",
        "main": "libs/services/src/index.ts",
        "tsConfig": "libs/services/tsconfig.lib.json",
        "assets": [],
        "project": "libs/services/package.json",
        "compiler": "swc",
        "format": ["cjs", "esm"]
      }
    },
    "lint": {
      "executor": "@nx/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": [
          "libs/services/**/*.ts",
          "libs/services/package.json"
        ]
      }
    }
  },
  "tags": []
}

While running the app via npx nx serve my-express-api is just working fine and the build npx nx build my-express-api does not produce any errors but tells me that everything went fine, i am not able to get that built bundle running with node main.js inside the folder where the bundle is located.

This is what happens:

Error: Cannot find module 'services'

I also added the paths in the tsconfig.base.json file:

"paths": {
      "services": ["libs/services/src/index.ts"],
      "shared-structures": ["libs/shared-structures/src/index.ts"]
    }

package.json of the monorepo:

{
  "name": "life",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {},
  "private": true,
  "devDependencies": {
    "@nx/esbuild": "16.7.4",
    "@nx/express": "^16.7.4",
    "@nx/jest": "16.7.4",
    "@nx/js": "16.7.4",
    "@nx/node": "^16.7.4",
    "@nx/rollup": "16.7.4",
    "@nx/webpack": "16.7.4",
    "@swc/cli": "~0.1.62",
    "@swc/core": "~1.3.51",
    "@types/express": "~4.17.13",
    "@types/jest": "^29.4.0",
    "@types/node": "^18.11.17",
    "concurrently": "^8.2.1",
    "esbuild": "^0.17.17",
    "jest": "^29.4.1",
    "jest-environment-node": "^29.4.1",
    "nx": "16.7.3",
    "nx-cloud": "latest",
    "prettier": "^2.6.2",
    "ts-jest": "^29.1.0",
    "ts-node": "10.9.1",
    "typescript": "~5.1.3"
  },
  "workspaces": [
    "apps/*",
    "libs/*"
  ],
  "dependencies": {
    "@swc/helpers": "~0.5.0",
    "axios": "^1.0.0",
    "dotenv": "^16.3.1",
    "express": "~4.18.1",
    "sequelize": "^6.6.5",
    "sequelize-cli": "^6.2.0",
    "tslib": "^2.3.0"
  }
}

nx.json:

{
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx/tasks-runners/default",
      "options": {
        "cacheableOperations": ["build", "lint", "test", "e2e"]
      }
    }
  },
  "extends": "nx/presets/npm.json",
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"]
    },
    "test": {
      "inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"]
    }
  }
}

Summary:

Expected Behavior

I'd expect the bundle to be able to get started via node main.js or pm2 start main.js. Neither works due to the error described above.

GitHub Repo

No response

Steps to Reproduce

  1. Create a package based mono repo
  2. create an express app via nx g @nx/express:app my-express-api
  3. create two libraries via nx g @nx/js:lib my-lib nx g @nx/js:lib my-lib2
  4. make my-lib a dependency of my-lib2 (in package.json: "my-lib": "*")
  5. build the project via nx build my-express-api
  6. start the bundle via node main.js (inside the folder where main.js is located

Nx Report

Node   : 18.12.1
   OS     : win32-x64
   npm    : 8.19.2

   nx             : 16.7.4
   @nx/js         : 16.7.4
   @nx/jest       : 16.7.4
   @nx/linter     : 16.7.4
   @nx/workspace  : 16.7.4
   @nx/devkit     : 16.7.4
   @nx/esbuild    : 16.7.4
   @nx/express    : 16.7.4
   @nx/node       : 16.7.4
   @nx/rollup     : 16.7.4
   @nrwl/tao      : 16.7.4
   @nx/webpack    : 16.7.4
   nx-cloud       : 16.3.0
   typescript     : 5.1.6

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

Note again, that serving via npx nx serve user-service works fine. Building via npx nx build user-service does not produce any errors and creates such folder structure:

dist
|-apps
|--user-service
|---main.js
|---main.js.map
|-libs
|--services (with all expected contents)
|--shared-structures (with all expected contents) 

But running node dist/apps/user-service/main.js results in:

node:internal/modules/cjs/loader:998 throw err; ^ Error: Cannot find module 'services'

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.