0

The below code snippet is from the watson tone analyzer documentation. The issue : when I paste the code and run the code it runs perfectly fine. However, when I paste it in another cloud function, it is stating that module of ibm-watson/auth cant be found. Not sure why it behaving differently ??

usually i save under the function main (params){}. However, the other cloud function is async function and it has the code for (require-promise). not sure what is wrong or what is the issue?

const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
const { IamAuthenticator } = require('ibm-watson/sdk');

const toneAnalyzer = new ToneAnalyzerV3({
  version: '2017-09-21',
  authenticator: new IamAuthenticator({
    apikey: 'apikey',
  }),
  serviceUrl: 'url',
});

const text = 'Team, I know that times are tough! Product '
  + 'sales have been disappointing for the past three '
  + 'quarters. We have a competitive product, but we '
  + 'need to do a better job of selling it!';

const toneParams = {
  toneInput: { 'text': text },
  contentType: 'application/json',
};

toneAnalyzer.tone(toneParams)
  .then(toneAnalysis => {
    console.log(JSON.stringify(toneAnalysis, null, 2));
  })
  .catch(err => {
    console.log('error:', err);
  });
    }
  • I did, cant run it, since not all parameters are required. its really comprehensive and I am not able to figure how to authenticate the service to start with using the function package. so I used the the snippet with reference to my first questions in this post. However, when I use it in cloud action, could not run it with other code snippet. I refer to other code as a master function where I am setting up some features and libraries and interact with watson assistant. the function is an action based on watson cloud function. don't have the experience to do the authentication. – Manah Hamadeh Mar 18 '21 at 15:01
  • I am more than happy to share the master function. however, I need some help figure out why at run time of the function, the watson-ibm/auth is not getting recorgnized. – Manah Hamadeh Mar 18 '21 at 15:02
  • I am using IBM cloud functions as a function by creating an action, pasting the code in the params function. – Manah Hamadeh Mar 19 '21 at 06:23
  • You are still not stating which runtime you are using. Depending on the runtime, the module is included or not. https://cloud.ibm.com/docs/openwhisk?topic=openwhisk-runtimes – data_henrik Mar 19 '21 at 07:03
  • thanks for pointing it out, I changed it to nodejs 12 and it worked – Manah Hamadeh Mar 21 '21 at 05:48
  • I cleaned up my comments and made it into an answer. Please mark as answered, so others can benefit. – data_henrik Mar 21 '21 at 07:25

1 Answers1

0

When working with IBM Cloud Functions, be aware of the different runtime environments and their included modules and packages. If you run into an error that a certain module is not found, you

data_henrik
  • 16,724
  • 2
  • 28
  • 49