Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless Framework: How to add external NPM packages?

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?

like image 782
taptipblard Avatar asked May 11 '16 17:05

taptipblard


People also ask

Does serverless deploy run npm install?

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.

What is npm install serverless?

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.

Which command installs the Serverless Framework correctly?

Open up a terminal and type npm install -g serverless to install Serverless.

What is SLS package?

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.


3 Answers

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.

like image 140
e_m0ney Avatar answered Oct 16 '22 07:10

e_m0ney


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

serverless-plugin-include-dependencies

like image 21
Maximi Avatar answered Oct 16 '22 07:10

Maximi


You can do the following:

# serverless.yml
custom:
  webpack:
    includeModules:
      packagePath: '../package.json' # relative path to custom package.json file.

Reference document

like image 38
Marcin Rapacz Avatar answered Oct 16 '22 07:10

Marcin Rapacz