Serverless Framework: How to add external NPM packages?

For me best solution was Serverless plugin: serverless-plugin-include-dependencies

serverless-plugin-include-dependencies


I think what you are experiencing is the same as what I was experiencing recently. I could install npm packages in my application root directory, but nothing would get deployed to lambda.

My understanding is that serverless deploys everything under each component directory (subdirectory under the application root). In your case, under functions.

I could not find much in the serverless documentation around this, but what I did was define a package.json file under my functions folder and then run an npm install in that subdirectory. Then after deploying to lambda, the node_modules under this directory got deployed too, meaning that my function code could require any of these npm modules.

So, your folder structure should now look like this:

root-project-folder
|-functions
|--package.json
|--node_modules
|---geopoint
|--geospatial
|---handler.js
|-package.json
|-node_modules
|--geopoint

The benefit here as well is that you can only deploy the npm dependencies that your functions need, without those that serverless needs to deploy your resources.

Hopefully that helps - once again, not sure this is best practise, just what I do because this isn't documented anywhere that I could find on the Serverless repository or in any example code.