Node.js install module

Not applicable

I want to install a node.js module using the management API .

Following this

http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisio...

and the steps mentioned by @arghya das here

http://community.apigee.com/questions/2903/how-to-add-modules-in-apigee-edge-node-proxy.html

I ran the GET npm dependencies and it says empty . I want to add a module sleep for example .

How do I do that ? I am confused on how I can add that using the management API . Any help will be appreciated .

Solved Solved
0 4 1,077
2 ACCEPTED SOLUTIONS

adas
New Member

You need to specify the dependencies in the package.json. When you run the POST /npm api with command=install, it will lookup the npm registry and download the node modules. Once you redeploy your proxy, those node modules would get installed and your nodescript would be able to call them.

View solution in original post

I am assuming that you used command=ls to get the dependencies and you see an empty object { }, right?

Do you have a package.json in your node resources?

If no, this is the first thing that you need to be able to execute any of the npm commands.

Create a new package.json file by doing the following

  1. Go to develop tab of your proxy
  2. Click New -> New Script
  3. Select File Type as "Node" and give a script name as "package.json" (without the quotes)
  4. Click Add
  5. Check that there is a package.json file under Scripts -> Node.js

Now declare the needed dependencies in this file.

You can use the following sample to add your dependency. In this case, I have added the module sleep.

{
  "dependencies" : {
    "sleep": ""
  }
}

Now when you run the management API with "command=ls", you will get notified that the dependency sleep is missing.

Now you can run the API with "command=install", and the latest version of this module will be downloaded and installed.

Once you have installed the modules using the management API, please redeploy the proxy to be sure that the new modules are being picked up by Node.

If you wish to do any changes to the dependencies or add/remove modules, then do the changes in the package.json file and then run the respective npm command.

The supported commands are [dedupe, install, ls, outdated, prune, update]

Hope this answers your question @Maruti Chand.

View solution in original post

4 REPLIES 4

adas
New Member

You need to specify the dependencies in the package.json. When you run the POST /npm api with command=install, it will lookup the npm registry and download the node modules. Once you redeploy your proxy, those node modules would get installed and your nodescript would be able to call them.

Yes it worked , thanks

I am assuming that you used command=ls to get the dependencies and you see an empty object { }, right?

Do you have a package.json in your node resources?

If no, this is the first thing that you need to be able to execute any of the npm commands.

Create a new package.json file by doing the following

  1. Go to develop tab of your proxy
  2. Click New -> New Script
  3. Select File Type as "Node" and give a script name as "package.json" (without the quotes)
  4. Click Add
  5. Check that there is a package.json file under Scripts -> Node.js

Now declare the needed dependencies in this file.

You can use the following sample to add your dependency. In this case, I have added the module sleep.

{
  "dependencies" : {
    "sleep": ""
  }
}

Now when you run the management API with "command=ls", you will get notified that the dependency sleep is missing.

Now you can run the API with "command=install", and the latest version of this module will be downloaded and installed.

Once you have installed the modules using the management API, please redeploy the proxy to be sure that the new modules are being picked up by Node.

If you wish to do any changes to the dependencies or add/remove modules, then do the changes in the package.json file and then run the respective npm command.

The supported commands are [dedupe, install, ls, outdated, prune, update]

Hope this answers your question @Maruti Chand.

Perfect , it works as expected. I don't see the steps to add package.json in the documents . Thanks.