Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@nrwl/nest - Generated package.json does not include @nestjs/platform-express starting with nx 15.2.2 #13625

Closed
swcm-mnestler opened this issue Dec 5, 2022 · 12 comments · Fixed by #13949
Assignees
Labels

Comments

@swcm-mnestler
Copy link

swcm-mnestler commented Dec 5, 2022

Current Behavior

From 15.2.2 onwards, using the generatePackageJson option in @nrwl/nest projects will not include @nestjs/platform-express in the generated package.json. As a result, when running the bundle with the generated dependencies only, Nest cannot start and prints an error

ERROR [PackageLoader] No driver (HTTP) has been selected. 
In order to take advantage of the default driver, please, ensure to install the "@nestjs/platform-express" package ($ npm install @nestjs/platform-express).

Generated package.json:

{
  "name": "nx-nestjs-platform-reproducer",
  "version": "0.0.1",
  "dependencies": {
    "@nestjs/common": "9.2.1",
    "@nestjs/core": "9.2.1",
    "reflect-metadata": "0.1.13",
    "rxjs": "7.6.0",
    "tslib": "2.4.1"
  },
  "main": "main.js"
}

Expected Behavior

For versions <=15.2.1, the @nestjs/platform-express dependency was included in the generated package.json. As a result, Nest was able to find the default HTTP driver.

{
  "name": "nx-nestjs-platform-reproducer",
  "version": "0.0.1",
  "dependencies": {
    "@nestjs/common": "9.2.1",
    "@nestjs/core": "9.2.1",
    "@nestjs/platform-express": "9.2.1",
    "reflect-metadata": "0.1.13",
    "rxjs": "7.6.0",
    "tslib": "2.4.1"
  },
  "main": "main.js"
}

Github Repo

https://github.com/swcm-mpilz/nx-nestjs-platform-reproducer

Steps to Reproduce

Steps to reproduce are described in https://github.com/swcm-mpilz/nx-nestjs-platform-reproducer/blob/main/run-reproducer.sh.
All that is required to reproduce the issue in a new repository:

  • npx create-nx-workspace nx-nestjs-platform-reproducer --preset=nest
  • Adapt project.json of the app to set "generatePackageJson": true
  • npx nx build
  • Inspect generated package.json: cat dist/apps/*/package.json
    To see the HTTP driver error:
  • Remove the workspace node_modules, otherwise node will find the missing module in the workspace root: rm -rf node_modules
  • Run server with the generated dependencies: cd dist/apps/* && npm i && node main.js

Nx Report

Node : 16.18.1
   OS   : linux x64
   npm  : 8.19.2
   
   nx : 15.2.4
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.2.4
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.2.4
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.2.4
   @nrwl/js : 15.2.4
   @nrwl/linter : 15.2.4
   @nrwl/nest : 15.2.4
   @nrwl/next : Not Found
   @nrwl/node : 15.2.4
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/webpack : 15.2.4
   @nrwl/workspace : 15.2.4
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Failure Logs

[Nest] ERROR [PackageLoader] No driver (HTTP) has been selected. In order to take advantage of the default driver, please, ensure to install the "@nestjs/platform-express" package ($ npm install @nestjs/platform-express).

Additional Information

Probably showing up now because the algorithm introduced in 15.2.2 - #13438 - thinks the dependency is an optional peer dependency

@Nick-Lucas
Copy link

Also seen here. Just waiting on a final shakedown when deployed, but the build artefacts came out fine after importing the dependency statically:

import '@nestjs/platform-express'

@Puertas
Copy link

Puertas commented Dec 7, 2022

Happening almost the same from same version 15.2.2 on Nextjs application with MUI/Emotion.

When build the project Emotion dependencies are not added to the package.json on dist folder, which create a runtime error. MUI is added, I suppose because there is a "direct" import on the project but Emotion, which is a MUI needed dependency not.

I think is the same issue, but let me know if this should be treat as an independent issue and I will create one.

Thanks in advance

@MichaelKaaden
Copy link

MichaelKaaden commented Dec 10, 2022

This problem hit me, too. I wrote a Bash script as an ugly workaround (or quick hack) to be run inside a "node:18-bullseye" container image on Linux. In my case, both @nestjs/platform-express and class-transformer are missing.

What this hack does is basically the following:

  • define the Nest.js app's directory after building it with nx build <app name> --prod
  • download jq as this is not part of the "node:18-bullseye" container image I'm using to build the app
  • read the versions of @nestjs/platform-express and class-transformer from the global package.json file
  • patch the package.json file in the Nest.js app's directory with this versions
#!/bin/bash
nestjs_dir=dist/apps/api
if [ -d ${nestjs_dir} ]; then
    wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
    mv jq-linux64 jq
    chmod u+x jq
    input_file=${nestjs_dir}/package.json
    platform_express_version=$(./jq -r '.dependencies."@nestjs/platform-express"' package.json)
    class_transformer_version=$(./jq -r '.dependencies."class-transformer"' package.json)
    echo patching ${input_file} with @nestjs/platform-expresss in version $platform_express_version
    echo patching ${input_file} with class-transformer in version $class_transformer_version
    cp ${input_file} api.tmp
    ./jq --arg platform_express_version "$platform_express_version" \
         --arg class_transformer_version "$class_transformer_version" \
         ".dependencies += {\"@nestjs/platform-express\": \"$platform_express_version\", \"class-transformer\": \"$class_transformer_version\"}" api.tmp > ${input_file}
    rm api.tmp
else
    echo Nothing to do.
fi

Maybe it'll help you in successfully building your app again.

@Nick-Lucas
Copy link

Also seen here. Just waiting on a final shakedown when deployed, but the build artefacts came out fine after importing the dependency statically:

import '@nestjs/platform-express'

Update on this solution. It works fine. Short of any fix to the root cause, the simplest thing is just to put a static import of each missing package in your app entrypoint and then Nx will pick it up and emit it.

The problem is very likely that Nx has stopped looking for dynamic imports, so we need to declare them.

@yharaskrik
Copy link
Contributor

Also seeing this now on 15.3.3 (just with @nestjs/platform-socket.io (express seems to work fine)

@meeroslav meeroslav self-assigned this Dec 21, 2022
@meeroslav meeroslav added the scope: core core nx functionality label Dec 21, 2022
@meeroslav
Copy link
Contributor

@Nick-Lucas, the solution you proposed works as a temporary workaround.

The difference in Nx is that we have stopped adding optional peer dependencies to the production bundles.
The @nestjs/platform-express is not a dependency of your app, but an optional peer dependency of @nestjs/core, and as such it was removed.

We will look into what is the best approach to solve this.

@yharaskrik your issue is related, the @nestjs/platform-socket.io is an optional peer dependency of @nestjs/websockets.

@meeroslav
Copy link
Contributor

meeroslav commented Dec 21, 2022

I think the solution is to consider root package json on how to treat optional peer dependencies - if they are added as a dependency in the root, then it will be resolved and mapped equally in the generated package.json.

Otherwise, we ignore it (to avoid bringing all kinds of unnecessary peer dependencies to generated package.json)

@tukusejssirs
Copy link

I have different problem: I don’t use @nestjs/platform-express in my apps (I use @nestjs/platform-fastify), but whenever I run nx migrate, it is always added to package.json.

This is happening to me for some while, but I can’t which Nx versions are affected. I know that nx@15.4.4 is affected.

Should I open a separate issue for this?

@meeroslav
Copy link
Contributor

@tukusejssirs check my last comment. Do you have @nestjs/platform-express in your root package.json? If yes, it will be added to generated package.json. If you don't want it, remove it from the root (unless that clashes with some other apps).

@ChristopherCapito
Copy link

ChristopherCapito commented Feb 9, 2023

I have a similar problem, but i am missing nest/core instead. The project is a vanilla project without any added bells and whistles. Clean generated @nrwl/nest application.

The generated package.json includes nestjs/common but not nestjs/core. any ideas how that could happen?

E: nestjs/core is added in the main package.json, just for reference.
E: actually the package from the OP is missing on my end as well. bummer

I would really like to use the generated package.json functionality as it reduces my Docker images by almost 500mb

For what its worth, the workaround with the static imports is working. But it is really undesirable from a dependency management perspective.

@meeroslav
Copy link
Contributor

We have just merged the improved lock file parsing and pruning, which is used for package.json generation. Hopefully, the new version solves it for you. Expect this fix in 15.7.0.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants