How am I supposed to use custom libraries in my nodejs project that runs in Apigee Edge?

Not applicable

Hi,

I want to make use of custom libraries in my apigee node js application. The custom library includes using Moment.js. I am using apigeetool to upload my application.

Even though the Moment library has been included in the zip, I am still unable to reference it in my application. How am I supposed to use custom libraries in my project?

1 2 1,901
2 REPLIES 2

@apigee1234567 It is best if you use NPM modules especially for standard libraries like moment.js

You can then require them normally as

var moment = require('moment');

In which case, you just have to ensure that the package.json is up-to-date with the dependencies and this is pushed to the proxy as well. Using a -R option with apigeetool will download and install all dependencies. apigeetool docs : https://www.npmjs.com/package/apigeetool In case you are bundling any libraries as .js files yourself, then you can include them in the root node folder, or in a subfolder (in which case it will be packaged as a zip file and uploaded to edge)

These can then be included like any other customer library as

var moment = require('./moment');

tl;dr if you aren't able to use the modules within your code, either it is not in package.json and hence not installed. Or if it is a custom library, then the path reference is wrong in the require.

Hope this helps.

If you have a custom library, not available in npm, you can use require. Just follow the normal nodejs rules. For example,

var myLibrary = require('./mylibrary.js');

This require will look in the resources directory for the file mylibrary.js, and will load it if found.

I think using apigeetool, it is not so simple to upload alternative JS source files. So... use apigeetool to load the main server file, then manually add the additional Javascript source file using the Edge UI.

Or, import and deploy your api proxy with a different tool, like pushapi.