Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add external npm modules #1104

Closed
taptip opened this issue May 11, 2016 · 3 comments
Closed

How to add external npm modules #1104

taptip opened this issue May 11, 2016 · 3 comments

Comments

@taptip
Copy link

taptip commented May 11, 2016

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?

@HyperBrain
Copy link
Member

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.

@taptip
Copy link
Author

taptip commented May 12, 2016

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?

@flomotlik
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants