My situation is that I am having a bit of trouble in adding external NPM packages to my Serverless Framework project (specific package is geopoint).
I went to the root folder of the Serverless project and ran npm install geopoint --save
. package.json got updated with dependencies": { "geopoint": "^1.0.1" }
and node_modules folder was created.
My folder structure looks like this:
root-project-folder
-functions
--geospatial
---handler.js
-node_modules
--geopoint
In my functions/geospatial/handler.js I declared the geopoint module with:
var geopoint = require('geopoint');
var geopoint = require('../../geopoint');
var geopoint = require('../../../geopoint');
The lambda console returns an error of:
{
"errorMessage": "Cannot find module '../../geopoint'",
"errorType": "Error",
"stackTrace": []
}
How can I properly add external NPM modules to a Serverless Framework project?
npm install is not ran automatically with the Serverless Framework. AWS provides a minimal node environment, but the only the aws-sdk is included. You'll need to run npm install before deploying.
The Serverless Framework is a command-line tool that uses easy and approachable YAML syntax to deploy both your code and cloud infrastructure needed to make tons of serverless application use-cases. It's a multi-language framework that supports Node. js, Typescript, Python, Go, Java, and more.
Open up a terminal and type npm install -g serverless to install Serverless.
The sls package command packages your entire infrastructure into the . serverless directory by default and make it ready for deployment. You can specify another packaging directory by passing the --package option. Copied.
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.
For me best solution was Serverless plugin: serverless-plugin-include-dependencies
serverless-plugin-include-dependencies
You can do the following:
# serverless.yml
custom:
webpack:
includeModules:
packagePath: '../package.json' # relative path to custom package.json file.
Reference document
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With