nrwl / nx

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

nx 18 + nest + webpack + serverless breaking with error src/main.handler is undefined or not exported #22875

Open pratik-github opened 6 months ago

pratik-github commented 6 months ago

Current Behavior

I am trying to setup serverless api using stack: nx 18 + nest + webpack + serverless node: 20.x

gives error:

{
    "errorType": "Runtime.HandlerNotFound",
    "errorMessage": "src/main.handler is undefined or not exported",
    "stack": [
        "Runtime.HandlerNotFound: src/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"
    ]
}

Expected Behavior

API should work

GitHub Repo

https://github.com/pratik-github/demo-nx-nest-serverless

Steps to Reproduce

  1. use node v20.x
  2. configure aws cli credentials
  3. npm install
  4. npx nx deploy graphql
  5. Deployment is successful but when I hit api endpoint I get error;

Internal server error image

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.9.0
OS     : darwin-arm64
npm    : 10.1.0

nx                 : 18.2.4
@nx/js             : 18.2.4
@nx/jest           : 18.2.4
@nx/linter         : 18.2.4
@nx/eslint         : 18.2.4
@nx/workspace      : 18.2.4
@nx/devkit         : 18.2.4
@nx/eslint-plugin  : 18.2.4
@nx/nest           : 18.2.4
@nx/node           : 18.2.4
@nrwl/tao          : 18.2.4
@nx/web            : 18.2.4
@nx/webpack        : 18.2.4
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/webpack/plugin
@nx/eslint/plugin
@nx/jest/plugin

Failure Logs

{
    "errorType": "Runtime.HandlerNotFound",
    "errorMessage": "src/main.handler is undefined or not exported",
    "stack": [
        "Runtime.HandlerNotFound: src/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

10.1.0

Operating System

Additional Information

verified by deploying to aws lambda

reference:

initial setup was done via `npx create-nx-workspace demoapp --preset=nest --bundler=webpack

jameelio commented 6 months ago

HI @pratik-github you can try the following add libraryTarget:"commonjs2" to the webpack.config.js image and remove webpack config from serverless.yml and also update function handler in serverless to the following handler: ./dist/src/main.handler image

jameelio commented 6 months ago

@pratik-github final three file changes I made image

Once completed run npx nx run graphql:build && npx nx run graphql:offline

pratiksyngenta commented 6 months ago

thanks @jameelio , I'll try these options and let you know

ugho49 commented 3 months ago

Thanks a lot, I had the same issue and adding libraryTarget:"commonjs2" worked for me 🙏