nrwl / nx

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

Cannot deploy nestjs backend to serveless computing solution like AWS lambda or firebase functions #20255

Open some1awesome opened 1 year ago

some1awesome commented 1 year ago

Current Behavior

After upgrading to nx 17.1.2 we can no longer deploy our project to AWS lambda or google firebase functions.

When uploading to firebase functions the firebase cli can't seem to find the firebase functions we are trying to upload.

When uploading to AWS the library is uploaded properly but when we try to hit the endpoint through api gateway we get an error saying it cannot find main.handler.

Expected Behavior

Projects should be able to be uploaded to firebase function and AWS lambda and still work properly once uploaded.

GitHub Repo

https://github.com/some1awesome/nestjs-upgrade-serverless-issue

Steps to Reproduce

  1. Run nx build

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 20.9.0
   OS     : darwin-arm64
   npm    : 10.1.0

   nx                 : 17.1.2
   @nx/js             : 17.1.2
   @nx/jest           : 17.1.2
   @nx/linter         : 17.1.2
   @nx/eslint         : 17.1.2
   @nx/workspace      : 17.1.2
   @nx/devkit         : 17.1.2
   @nx/eslint-plugin  : 17.1.2
   @nx/nest           : 17.1.2
   @nx/node           : 17.1.2
   @nrwl/tao          : 17.1.2
   @nx/webpack        : 17.1.2
   typescript         : 5.2.2

Failure Logs

When uploading to firebase functions:
The following functions are found in your project but do not exist in your local source code:
        testApi(us-central1)

When trying to hit aws lambda:
"Runtime.HandlerNotFound: dist/apps/tools-server/main.handler is undefined or not exported",
        "    at UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1122:15)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)",
        "    at async file:///var/runtime/index.mjs:1288:1"

Package Manager Version

No response

Operating System

Additional Information

We have isolated the problem to be due to the latest upgrade of @nx/webpack, if we run nx migrate @nx/webpack --to="@nx/webpack@16.10.0" it begins to work properly again.

It seems to be due to the absence of the following lines of code:

var __webpack_export_target__ = exports; for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i]; if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, "__esModule", { value: true }); /******/ })()

If after running the build those lines are added in then we can upload and run the server properly, but the original build does not include those lines.

If you want to fully test it you will need a blaze plan firebase account. Then you can change the firebase.json to point at that account and try uploading the testApi. To do so run the following:

1) npm run build:functions 2) firebase deploy --only functions:testApi

mahmok-lego commented 1 year ago

I am also experiencing the same issue

joain946 commented 1 year ago

I also have the same issue. It works with "17.1.0-beta.4" but not with "17.1.1".

JumBay commented 1 year ago

Same with 17.1.3

gabrielgagne commented 11 months ago

Having the same problem with a vscode extension built with

"targets": { "build": { "executor": "@nx/webpack:webpack", "options": { "outputPath": "dist/apps/wkc-web-vs-code-extension", "main": "apps/wkc-web-vs-code-extension/src/main.ts", "tsConfig": "apps/wkc-web-vs-code-extension/tsconfig.app.json", "assets": [ "apps/wkc-web-vs-code-extension/src/assets", "apps/wkc-web-vs-code-extension/src/package.json", "apps/wkc-web-vs-code-extension/src/LICENSE.txt" ], "target": "node", "compiler": "tsc" }, "configurations": { "production": { "optimization": true, "extractLicenses": true, "inspect": false, "fileReplacements": [ { "replace": "apps/wkc-web-vs-code-extension/src/environments/environment.ts", "with": "apps/wkc-web-vs-code-extension/src/environments/environment.prod.ts" } ] } }, "outputs": ["{options.outputPath}"] }, }

By reverting some update I can see that it started after updating to 17.1.1 from 17.0.3

empro-dveitinger commented 8 months ago

We were able to resolve this issue by adding the following to our webpack.config.ts: output: { libraryTarget: 'commonjs2', },

github-actions[bot] commented 2 weeks ago

This issue has been automatically marked as stale because it hasn't had any activity for 6 months. Many things may have changed within this time. The issue may have already been fixed or it may not be relevant anymore. If at this point, this is still an issue, please respond with updated information. It will be closed in 21 days if no further activity occurs. Thanks for being a part of the Nx community! 🙏