aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

ESBuild does not load file with dynamic require? #4550

Closed Maoragai12 closed 1 year ago

Maoragai12 commented 1 year ago

Description:

I'm using one of the resources and the methods of the Shopify SDK. When I build the Lambda I get no errors and everything is ok. After I deploy the Lambda and trigger it, I get the following error:

Error: Cannot find module './order'
Require stack:
- /var/task/app.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at G.get (/private/var/folders/cf/ggmwqfwn03q443mj6vzryn0c0000gr/T/tmp4iq4d0no/node_modules/shopify-api-node/resources/index.js:87:26)
    at Runtime.gv (/private/var/folders/cf/ggmwqfwn03q443mj6vzryn0c0000gr/T/tmp4iq4d0no/app.ts:11:35)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1089:29) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/var/task/app.js', '/var/runtime/index.mjs' ]
}

After some digging in the code, I understand that one of the methods in the index file of the SDK use dynamic require, and that's what calls the ./order file.

The build configuration in the template.yaml:

  Metadata:
    BuildMethod: esbuild
    BuildProperties:
      Minify: true
      Target: "es2020"
      Sourcemap: true
      EntryPoints: 
        - app.ts

When I use makefile of my own with webpack instead of esbuild, everything works as expected.

There is any problem with dynamic require in esbuild?

Steps to reproduce:

  1. Install the Shopify SDK
  2. Use one of the resources
  3. Build and Deploy the Lambda
  4. Trigger the Lambda

Expected result:

The Lambda should be triggered with no errors.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS:
  2. sam --version: 1.65.0
  3. AWS region: us-east-1
hawflau commented 1 year ago

Hi @Maoragai12, thanks for raising an issue.

After looking up for a bit, unfortunately, it seems like a limitation of ESBuild [1], and Shopify-api-node was not designed to use with a bundler like ESBuild [2].

[1] - https://esbuild.github.io/api/#non-analyzable-imports [2] - https://github.com/MONEI/Shopify-api-node/issues/506

[1] suggested a workaround though:

The way to work around this issue is to mark the package containing this problematic code as external so that it's not included in the bundle. You will then need to ensure that a copy of the external package is available to your bundled code at run-time.

Maoragai12 commented 1 year ago

Hi @Maoragai12, thanks for raising an issue.

After looking up for a bit, unfortunately, it seems like a limitation of ESBuild [1], and Shopify-api-node was not designed to use with a bundler like ESBuild [2].

[1] - https://esbuild.github.io/api/#non-analyzable-imports

[2] - https://github.com/MONEI/Shopify-api-node/issues/506

[1] suggested a workaround though:

The way to work around this issue is to mark the package containing this problematic code as external so that it's not included in the bundle. You will then need to ensure that a copy of the external package is available to your bundled code at run-time.

Hi, thanks for the explanation!

Regarding the suggestion - how can I include the package at runtime? There is any reference you can share please?

mildaniel commented 1 year ago

Hey @Maoragai12, apologies for the late response. One way of including a dependency after marking it as external from esbuild is to then create a Lambda Layer Version resource in your CFN stack that contains your dependency.

I will close this for now since this doesn't seem to be an AWS SAM CLI issue, but feel free to open a new issue if anything else comes up!

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

reecefenwick commented 11 months ago

@Maoragai12 late to the party, but I have actually raised a PR to shopify-api-node to address this issue