How to include a javascript... from Apigee nodejs node

Not applicable

I've looked at the docs and this works on standalone nodejs...

I've added a javascript called "clobs.js"

and reference it in my endpoint js as

var clobs = require('jsc://clobs.js')

.or './clobs.js' or 'clobs.js' or '../jsc/clobs.js' or several other variations

but always taking an error

{"fault":{"faultstring":"Script node executed prematurely: Error: Cannot find module 'clobs'\n    at module.js:340\n    at module.js:280\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/quick-app-main.js:3\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:497\n    at startup (trireme.js:142)\n    at trireme.js:923\n","detail":{"errorcode":"scripts.node.runtime.ScriptExitedError"}}}
Solved Solved
3 6 1,574
1 ACCEPTED SOLUTION

Not applicable

Hi, @Gervase Gallant, the issue you're facing is because require function expects you to point it to either a location in the filesystem or a module from node_modules folder. JSC notation used from JavaScript Policies is not understood by Node.js. So, you need to upload clobs.js file to Apigee along with app.js and any other dependencies, unless you use Manage Node Package Modules Mgmt api, which in your case is not need.

You should try apigeetool, which imports all JS files along with any files and folders within the same directory. So your require call should be like this if the file is in the same directory as the app.js file:

var clobs =require('./clobs.js');

The command to import Node.js apps into Apigee with Apigeetool should look like this:

apigeetool deploynodeapp -n apigeehelloworldnodejs -d . -m app.js -b /apigeehelloworldnodejs -u $ae_username -o testmyapi -e test -p $ae_password 

For more info, check apigeetool npm.

View solution in original post

6 REPLIES 6

Not applicable

Hi, @Gervase Gallant, the issue you're facing is because require function expects you to point it to either a location in the filesystem or a module from node_modules folder. JSC notation used from JavaScript Policies is not understood by Node.js. So, you need to upload clobs.js file to Apigee along with app.js and any other dependencies, unless you use Manage Node Package Modules Mgmt api, which in your case is not need.

You should try apigeetool, which imports all JS files along with any files and folders within the same directory. So your require call should be like this if the file is in the same directory as the app.js file:

var clobs =require('./clobs.js');

The command to import Node.js apps into Apigee with Apigeetool should look like this:

apigeetool deploynodeapp -n apigeehelloworldnodejs -d . -m app.js -b /apigeehelloworldnodejs -u $ae_username -o testmyapi -e test -p $ae_password 

For more info, check apigeetool npm.

Also, take a look at this Apigee doc topic for more examples: Deploying a standalone Node.js app. If your Node app is set up correctly and working locally, then it will work when deployed on Edge with apigeetool.

@dzuluaga, @wwitman Thanks for the input.

I have been typically using Nodejs in apigee to get quick rest endpoints prototypes out to support development. So I have been developing from a local nodejs instance and moving the app by simply creating files in the apigee dashboard.

Going forward I'll download the apigeetool app and deploy from my local.

In other endpoints, I was able to create new "node" js files and reference them just as you illustrate.

var clobs =require('./clobs.js');

However, this doesn't work for "javascript" files. These are stores in a /jcs folder, I can't figure the direct url to load them.

JSC and Node.js files aren't shared. The reason being is that JSC Policies are plain old JS files, which aren't declared as CommonJS modules, which put their functionality in the module.exports namespace and are usually included using require(pathToModule) in a server-side script. However, it's an interesting question since you conceivably could leverage other libraries such as Browserify (I haven't tried it though) to try to write JSC policies to achieve more reusability.

Another possible solution is to copy JSC policy files to the Node.js app folder during API bundle build time. You can achieve that by leveraging other build tools such as Apigee Grunt Plugin, which can do this for you and a lot more. Yeoman API Proxy Generator can help you build the basic structure in a matter of seconds.

Please let me know if you have any questions.

Not applicable

Hi,

I've created a small module that uses browserify to bundle up your node modules so they can be used in a JS policy in Apigee...

You can find it here: https://www.npmjs.com/package/policify

Awesome! Thanks for sharing @rluitwieler!