I have a node_modules folder from there i want to exclude only few modules but other modules should be added while packaging. How can i do that.Also how to exclude some modules using regex. like in my node_modules I have created custom modules that starts with md-request, md-models etc. so i want to exclude only these modules from packaging. Thanks in advance.
It's most likely you're trying to exclude dev-dependencies. Install your dev dependencies using npm i some-dependency --save-dev
and, by default, Serverless will not pack them into the final artifact.
If that's not sufficient, you can exclude everything and include only what you need.
You can see how Serverless behaves when packaging an artifact as well as seeing how to exclude/include dependencies on the official docs
Here's an example (extracted from their docs) to exclude node_modules but to keep node_modules/node-fetch (which is pretty much what you're looking for):
package:
exclude:
- node_modules/**
- '!node_modules/node-fetch/**'
And this is how you can include only what you need:
package:
exclude:
- src/**
include:
- src/function/handler.js
EDIT: after the OP's comment, here's how to handle the desired behaviour:
package:
exclude:
- node_modules/my_module_1/**
- node_modules/my_module_2/**
- node_modules/my_module_3/**
- node_modules/my_module_4/**
If the module names are very close to each other like the above, you can use the wildcard *
to exclude them all in a one-liner:
package:
exclude:
- node_modules/my_module_*/**
EDIT 2: Here's a working example (see that I am excluding aws-sdk):
package:
exclude:
- node_modules/aws-sdk/**
Since the package is too large and I am not able to see it through AWS Lambda's console, I am attaching two screenshots - one with exclude and one without exclude (see how the package size changes).
With exclude:
Without exclude:
This way you don't even need to add the '!node_modules/whatever-i-want-to-keep' argument
EDIT 3: Using the *
wildcard in a sample project with manually created node_modules and respective my_module_* directories