Node JS Code works on Local but Fails on Apigee with Runtime Error : ScriptExitedError

sydub7
Participant IV

The following code works on Local just fine, however throws a runtime exception when deployed on Apigee Edge. The modules in package.json are uploaded fine.

Steps

1. Created server.js on local
2. Created package.json using npm init
3. npm install <packagename> --save all three packages on local. They install & package.json gets updated.
4. Run the App on local and it works fine. (When I post json object using postman, the data gets inserted in Baas)

5. Deployed the App on Edge using apigeetool deploynodeapp
6. Deployed with success and all modules (zip files), package.json & server.js are uploaded and deployed as target for edge proxy
7. When trying to post data on edge proxy it gives a runtime exception (below).

Am I missing a step here ? Please advise.


Thanks

Syd

Error

{
    "fault": {
        "faultstring": "Script node executed prematurely: TypeError: Cannot find function assign in object function Object() { [native code for Object.Object, arity=1] }\n.\nTypeError: Cannot find function assign in object function Object() { [native code for Object.Object, arity=1] }\n.\n    at /organization/environment/api/node_modules/node-rest-client/lib/nrc-parser-manager.js:112\n    at /organization/environment/api/node_modules/node-rest-client/lib/node-rest-client.js:10\n    at /organization/environment/api/server.js:4\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"
        }
    }
}

server.js

var express = require('express');
var app = express();
var Client = require('node-rest-client').Client;
var httpClient = new Client();

var bodyParser = require("body-parser");
app.use(bodyParser.json());

app.get('/', function(request, response){
    response.type('application/json');
    response.send(JSON.stringify({message:'hello world', status:'200 OK'}));
});


app.post('/orders', function(request,response){
    response.type('application/json');
    var data = request.body

    var args = {
        data: {test:data} ,
        headers: { "Content-Type": "application/json" }
    };
httpClient.post("https://apibaas-trial.apigee.net/<myorg>/sandbox/orders", args, function (data, res){ console.log(res.statusCode); }); response.send(JSON.stringify({message:'Data was created', status:'200 OK'})); }); app.put('/update', function(request,response){ response.type('application/json'); response.send(JSON.stringify({message:'Data was updated', status:'200 OK'})); }); app.delete('/delete', function(request,response){ response.type('application/json'); response.send(JSON.stringify({message:'Data was deleted', status:'200 OK'})); }); var server = app.listen(9000, function(){ console.log('Node server is running..'); });

package.json

{
  "name": "node2",
  "version": "1.0.0",
  "description": "my node server",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "node-rest-client": "^3.1.0"
  }
}
Solved Solved
0 6 996
2 ACCEPTED SOLUTIONS

Object.assign is not available in Node.js v0.10.x. If you test locally with Node.js v0.10.x, you should get the same error you're seeing when deploying on Edge.

View solution in original post

6 REPLIES 6

@Syd looks like one of your dependencies may not be allowed check this post : https://community.apigee.com/questions/45790/comprehensive-list-of-unaccepted-node-modules.html

Syd, you said it "works on Local" and that you "Ran the app on local". But have you tried running it locally within trireme?

To do it, you can do this:

sudo npm install -g trireme
trireme <your script> 

More info here.

I'm guessing you will get some additional diagnostic info, pinpointing the problem suggested by Christin.

Thanks for the directions @Dino. I followed your instructions, but the results are same. It works on local (now with trireme too), however refuses to work on Edge. Here is my updated package.json file. I can see all the modules (including trireme) being uploaded fine on Edge.
screen-shot-2017-11-27-at-92831-pm.png

screen-shot-2017-11-27-at-92644-pm.png

Also as @Christin Brown suggested, I figured that the problem lies with "node-rest-client": "^3.1.0" . If I remove this package, everything works fine on Edge too. Looks like i need to find a different package to post data in Baas.

Thanks again gentlemen.

Regards

Syd

{
  "name": "node2",
  "version": "1.0.0",
  "description": "my node server",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "node-rest-client": "^3.1.0",
    "trireme": "^0.9.0"
  }
}
<br>

Object.assign is not available in Node.js v0.10.x. If you test locally with Node.js v0.10.x, you should get the same error you're seeing when deploying on Edge.

Thanks @Jeremy Whitlock. This means there a significant gap when it comes to node versions. The version installed on my machine is 6.x where as the version on Edge is 0.10.x
Which explains your point.