Development Dependencies not Excluded

Hi All,

New to the board and I am also a bit of a novice with serverless, especially v4.

Apologies in advance for any breaches in etiquette. :grinning:

I am using:

Serverless ϟ Framework 4.4.11
NPM 10.8.2
Node 20.18.0

The project I am packaging deploying has a node runtime:

  runtime: nodejs20.x

My expectation is that the lambda functions being packaged individually will not include the packages listed in “devDependencies” of the projects package.json in the final package but that is not happening.

Originally, I had assumed that serverless would run a fresh npm i --production as an intermediary step during build. I’ve observed this not to be the case. Instead, it looks as if the existing node_modules directory is packaged (or not, if it does not exist) and that there is an intermediary build step that will “exclude any existing dev packages”:

I can see that the package process usually hangs here for a while because, I assume, it takes time to parse out and remove those development packages.

However, when I check in node_modules directory of the packaged function, it is clear that the dev packages are still in node_modules.

So, in order to get a “clean” package I am left to manually remove the node_modules directory and do a fresh install npm i --production before running the package command.

Is this the standard and suggested workflow? Or, is something wrong here?

Thanks!
Justin

Here is what the relevent serverless.yml config looks like:

package:
  individually: true
  excludeDevDependencies: true

functions:
  generate:
    name: ${self:service}-${sls:stage}-generator
    handler: generate.handler
    package:
      exclude:
        - "node_modules/@aws-sdk/**"
        - "node_modules/aws-sdk/**"
        - ".github/**"
        - ".gitignore"
        - ".vscode/**"
        - "test/**"
        - "tmp/**"
  invoke-generate:
    handler: invoke-generate.handler
    package:
      include:
        - invoke-generate.js
      exclude:
        - "**/*"
    events:
      - http:
          path: generate
          method: post