sergioramos / serverless-plugin-tree-shake

Shake the dependency tree and only package files needed
BSD 3-Clause "New" or "Revised" License
24 stars 11 forks source link

Error: Cannot find module 's_index' when serverless enterprise is enabled #47

Open artworx opened 4 years ago

artworx commented 4 years ago

This is my serverless.yml file:

...
 functions:
    index:
      handler: dist/index.handler
...

The deployed Lamba's entry point gets set to s_index.handler, but when serverless-plugin-tree-shake is enabled, that file is not present in the deployment.

  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              12.18.1
     Framework Version:         2.9.0 (standalone)
     Plugin Version:            4.1.1
     SDK Version:               2.3.2
     Components Version:        3.2.7
sergioramos commented 4 years ago

thanks for the report, I'm a bit busy right now, but I'll check this out once I have some time.

One thing to check: do you have any plugin compiling the files into dist/index.js? If so, is that step happening when you add serverless-plugin-tree-shake?

artworx commented 4 years ago

I don't have any extra plugins for compiling

This is the minimal serverless.yml I used to reproduce it.

org: artworx
app: demo
service: lambda-demo

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-central-1
  stage: dev

plugins
  - serverless-plugin-tree-shake

functions:
  hello:
    handler: handler.main

As soon as I add org and app keys to serverless.yml, the handler in cloud formation is s_handler.

# .serverless/serverless-state.json

          "HelloLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
              "Code": {
                "S3Bucket": {
                  "Ref": "ServerlessDeploymentBucket"
                },
                "S3Key": "serverless/...
              },
              "Handler": "s_hello.handler",

If I remove the serverless-plugin-tree-shake, the packaged zip file includes s_handler.

edit: without the plugin:

Archive:  .serverless/demo-lambda.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     158  Defl:N      123  22% 1980-01-01 00:00 9d69e4e5  handler.js
     177  Defl:N      139  22% 1980-01-01 00:00 550be662  node_modules/.bin/components
    1542  Defl:N      694  55% 1980-01-01 00:00 6339c529  node_modules/.bin/nft
  272492  Defl:N    83162  70% 1980-01-01 00:00 85af2074  package-lock.json
     431  Defl:N      263  39% 1980-01-01 00:00 71d54573  package.json
     910  Defl:N      490  46% 1980-01-01 00:00 79514b6d  s_hello.js
  350564  Defl:N   121712  65% 1980-01-01 00:00 0fb2e8c6  serverless_sdk/index.js
--------          -------  ---                            -------
  626274           206583  67%                            7 files

with the plugin enabled:

Archive:  .serverless/demo-lambda.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     158  Defl:N      123  22% 1980-01-01 00:00 9d69e4e5  handler.js
     431  Defl:N      263  39% 1980-01-01 00:00 71d54573  package.json
--------          -------  ---                            -------
     589              386  35%                            2 files

Original directory:

$ tree
.
├── handler.js
├── package.json
├── package-lock.json
└── serverless.yml
sergioramos commented 4 years ago

But notice that in your second snippet you no longer have the dist/ in the handler. Any reason why?

artworx commented 4 years ago

Because i created a new project with just a single js file. It's not about the compilation, its about the injected wrapper by the serverless framework which gets stripped from the final package.

On Tue, Nov 10, 2020, 6:43 PM Sérgio Ramos notifications@github.com wrote:

But notice that in your second snippet you no longer have the dist/ in the handler. Any reason why?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sergioramos/serverless-plugin-tree-shake/issues/47#issuecomment-724823437, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAO5GEVYFMGIJZM27QV7BULSPFUSZANCNFSM4TPSASTQ .