Unable to recognize node modules

Not applicable

I am trying to run a node js project in apigee and I require a module. I added it in package.json installed it locally and zipped the nodemodules file and uploaded. Still when running the test I am getting the following error "Error: Cannot find module 'request-promise' at module.js" . Quite confused. Any help would be appreciated..Thanks..

0 11 1,122
11 REPLIES 11

@Annapoorna Krishnamoorthy ,

Welcome to Apigee Community,

Have you tried npm install using API here ? Keep us posted.

Hi Anil Yes i did npm install and zipped the node modules

2956-capture.png

and uploaded in the proxy bundle

@Annapoorna Krishnamoorthy , Can you re-run the npm install on Apigee Cloud using above API to re-generate dependencies that your application needs ? Let me know your orgname, i can verify same. You can pm me the orgname using "Ask An Expert" button on right side bar.

Hey Anil Thanks a lot!!. It worked when I downloaded using the above link.. But should we do the same everytime? .Can I not use the locally installed zip to satisfy the dependencies? This is the line of code i have used to use the module:

var rp = require('request-promise');

And this is the name of the zip folder of node modules

node_modules_request-promise.zip

Not applicable

Hi @Annapoorna Krishnamoorthy, I've run into some issues that might be related to what you're not able to run request-promise. In order to workaround this issues, I converted request based on callback into a promised-based by leveraging Bluebird framework. I created a sample API Proxy, which you can use right away from this Git repo https://github.com/dzuluaga/nodejs-request-promise-api.

Hope it helps! Let me know how it goes!

============================================

nodejs-request-promise-api

Here's an example of a promise-enabled Node.js API Proxy ready to be deployed in Apigee.

Why promises?

Node.js promises subject has been documented extensively by many tutorials online. So, it's out of the scope of this sample to explain the (why)[https://alexperry.io/node/2015/03/25/promises-in-node.html] here. However, I noticed there are few examples of Apigee API Proxies explained with working code. So, I decided to write rather a contrived example to be used as a reference for new developers coming to the platform. So, next time they need to write one, you don't have to start from scratch. Hope it helps!

Show me the code?

var express = require('express')
    app = express(),
    request = require('request'),
    url = require('url'),
    promise = require('bluebird'), // you'll need bluebird or other module to convert callbacks to promises
    request = promise.promisify(require('request')); // promisify the request module

app.get('/*', function (req, res) {
  var query = url.parse(req.url).query; //get query params from url
  request('http://lyrics.wikia.com/api.php?' + query) // use promisified request module
    .then(function (response) { //this is way clean than using callbacks!
      res.json(JSON.parse(response.body));
    })
    .catch(function (err) {
      res.send(err);
    })
})

How can I deploy this API Proxy?

deploy with Deploy Now

Deploy this Node.js app to Apigee with a single click, click below:

Deploy to Apigee

deploy with apigeetool

The following command will deploy this Node.js App to Apigee. apigeetool deploynodeapp -u $ae_username -p $ae_password -o testmyapi -e test -n music-nodejs-request-promise-api -d . -m app.js -b /music-nodejs-request-promise-api -v default -V -U

How can I test this API Proxy?

Call this API by using this sample URL:

$ curl "http://testmyapi-test.apigee.net/music-nodejs-request-promise-api?action=lyrics&artist=radiohead&fmt=json"

@Diego Zuluaga Thanks a lot. I m learning promises and I shall try your solution as well..

Not applicable

According to Apigee documentation:

http://docs.apigee.com/api-services/content/understanding-edge-support-nodejs-modules

Modules that depend on EcmaScript 6 features, such as Promises and Generators, are not supported.

Thanks for chiming in @José Ángel Gariburo. You're right, Promises or ES6 is not currently natively supported. However, you can use polyfills or transpilers to fill the gap. In the example above, I'm using Bluebird to promisify request module. Or you can also leverage other Polyfill modules such as https://github.com/getify/native-promise-only.

I haven't tried all ES6 syntax on Apigee. However, you can also transpile ES6 to ES5 with the help of Babeljs.

@docs - Perhaps it'd be possible to clarify other options to support some ES6 features. Even those options aren't native.

@Diego Zuluaga, I hesitate recommending other options in the docs unless they're lasting options. Node.js modules and versions can move pretty quickly, so keeping the docs current would be a challenge--again, unless there are some fairly permanent examples you can give us.

Agreed @Floyd Jones. Node.js modules move pretty quick, and it's hard to keep up with them. However, the message of no support at all vs. support with the help of NPM is more encouraging. @Jeremy Whitlock, @Greg Brail any thoughts on this?

We at Apigee have tested extensively these options internally and externally in multiple projects. E.g. Nucleus APIs and customer APIs. I have also provided some examples on how to Promisify functions based on callbacks.

Here are some samples that I've written to showcase use of Promises with Apigee API Proxies:

Thanks!

Transpiling and polyfills should always be an option, assuming the generated code or polyfill works in the node.js version that Trireme is locked to, which is 0.10.x. So documenting this seems reasonable to me.