'Can't exclude node_modules directory during serverless packaging

I've successfully configured a separate serverless layer for nodejs that contains all my apps node_modules.

The separate layer zip file built during serverless package correctly contains what I would expect (i.e. only node_modules).

I then have my package.patterns setup like the below, yet no matter what I do, my main service's zip file STILL contains node_modules. I've tried explicity doing excludes: and includes: to no success either.

Because node_modules continues to be packaged in the main function's zip package, I can't view the function in the lambda web interface because it's too large to display.

Anyone have any ideas for how to properly have my node_modules in a separate layer and get them OUT of the primary function package? I'm using serverless 0.70.0

serverless.yml snippet

layers:
  nodeModules:
    name: ${self:service.name}-${opt:stage,'dev'}-nodeModules
    package:
      include:
        - ./**
    path: lambda_layers
    compatibleRuntimes:
      - nodejs14.x

...

package:
  patterns:
    - '!**'
    - '!node_modules/**'
    - utils
    - validation
    - '*.js'layers:



Solution 1:[1]

the issue turns out to be this plugin messing it up, after removing it, all the packaging directives work fine. Removing the usage of serverless-plugin-include-dependencies and everything works fine.

Solution 2:[2]

I was also facing the same issue, node_modules were being included even after excluding it, and using plugins such as the serverless_ignore plugin also did not help.

in the end, this config worked for me.

package:
  individually: true
  exclude:
    - tables
    - package.json
    - package-lock.json
  include:
    - '!node_modules/**'

try this. (serverless.yml) the major change is I am using include now, and then blob pattern sign (!) to ignore node_modules

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Abhay Soni