Serverless: How to add external npm modules

Created on 11 May 2016  Β·  3Comments  Β·  Source: serverless/serverless

Hi,

I have seen similar existing issues but I can't seem to make it work. I would like your advice if what im doing is right.
This is what my folder structure looks like:

.
β”œβ”€β”€ admin.env
β”œβ”€β”€ functions
β”‚Β Β  └── geospatial
β”‚Β Β   Β Β  └── get_bounding_coordinates
β”‚Β Β   Β Β      β”œβ”€β”€ event.json
β”‚Β Β   Β Β      β”œβ”€β”€ handler.js
β”‚Β Β   Β Β      └── s-function.json
β”œβ”€β”€ _meta
β”‚Β Β  β”œβ”€β”€ resources
β”‚Β Β  β”‚Β Β  └── s-resources-cf-dev-uswest2.json
β”‚Β Β  └── variables
β”‚Β Β      β”œβ”€β”€ s-variables-common.json
β”‚Β Β      β”œβ”€β”€ s-variables-dev.json
β”‚Β Β      └── s-variables-dev-uswest2.json
β”œβ”€β”€ node_modules
β”‚Β Β  └── geopoint
β”‚Β Β      β”œβ”€β”€ geopoint.js
β”‚Β Β      β”œβ”€β”€ index.js
β”‚Β Β      β”œβ”€β”€ LICENSE
β”‚Β Β      β”œβ”€β”€ package.json
β”‚Β Β      β”œβ”€β”€ README.md
β”‚Β Β      └── test
β”‚Β Β          β”œβ”€β”€ test.accessors.js
β”‚Β Β          β”œβ”€β”€ test.bounding.js
β”‚Β Β          β”œβ”€β”€ test.constructor.js
β”‚Β Β          β”œβ”€β”€ test.conversions.js
β”‚Β Β          └── test.distance.js
β”œβ”€β”€ package.json
β”œβ”€β”€ s-project.json
β”œβ”€β”€ s-resources-cf.json
└── s-templates.json

First on the root of my project folder, I ran "npm install geopoint --save". it added "dependencies": { "geopoint": "^1.0.1" } on package.json. It also added the npm_modules folder and its contents.

In functions/geospatial/get_bounding_coordinates/handler.js, i declared var geopoint = require('geopoint');

The error is that lambda returns:

{
  "errorMessage": "Cannot find module 'geopoint'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._load (module.js:276:25)",
    "Module.require (module.js:353:17)",
    "require (internal/module.js:12:17)",
    "Object.<anonymous> (/var/task/handler.js:2:16)",
    "Module._compile (module.js:409:26)",
    "Object.Module._extensions..js (module.js:416:10)",
    "Module.load (module.js:343:32)",
    "Function.Module._load (module.js:300:12)",
    "Module.require (module.js:353:17)"
  ]
}

Is there something wrong with my implementation?

Most helpful comment

In your s-function.json you have to specify your handler as functions/geospatial/get_bounding_coordinates/handler.handler. This will set the function root to the directory containing the node_modules. From my experience it is better to have the function root in a subfolder of the project, as otherwise all serverless plugins will be also packaged.

All 3 comments

In your s-function.json you have to specify your handler as functions/geospatial/get_bounding_coordinates/handler.handler. This will set the function root to the directory containing the node_modules. From my experience it is better to have the function root in a subfolder of the project, as otherwise all serverless plugins will be also packaged.

Hi! Thank you very much for the answer! It worked! What do you mean by setting the function root in the subfolder? is it forexample putting the node_modules folder and package.json file under functions folder?

Hi @taptip would be great if we could move this discussion into our Gitter chat so we can have support questions there. We will make our documentation a lot better to cover this in the future.

I will close the issue for now to mark it as resolved.

Was this page helpful?
0 / 5 - 0 ratings