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?
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.
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 thenode_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.