0
votes

I'm using IBM Cloud functions for requesting some results from IBM Watson Discovery. There is a very good GitHub repo here about how can we invoke this. However, on the way of setting the action in the function an error with message Cannot find module 'ibm-watson/auth' occurs. I understand what the message says, but if you check the GitHub repo, you see that I apply exactly the same steps. My IBM Cloud function code is:

case 'doc_info':
            var user_input = params.user_input;
            const watson = require('ibm-watson/sdk');
            const { CloudantV1 } = require('@cloudant/cloudant');
            const DiscoveryV1 = require('ibm-watson/discovery/v1');
            const { IamAuthenticator } = require('ibm-watson/auth');
            //const { IamAuthenticator } = require('ibm-cloud-sdk-core');

            try {
                const discovery = new DiscoveryV1({                 // Initializing Discovery
                    version: '2020-11-24',
                    authenticator: new IamAuthenticator({
                        apikey: params.apiKeyDisco,
                    }),
                    serviceUrl: params.urlDisco,
                });

                const cloudant = new CloudantV1({                   // Initializing CloudantV1
                    authenticator: new IamAuthenticator({
                        apikey: params.apiKeyCloudant
                    })
                });
                cloudant.setServiceUrl(params.urlCloudant);
                 
                 // ...
                 return {
                   answer: "Simple test"
                 };
                
            } catch (err) {
                console.log(err)
                return Promise.reject({
                  statusCode: 500,
                  headers: { 'Content-Type': 'application/json' },
                  body: { message: err.message },
                });
            }
            break;

I've tried even with calling IamAuthenticator from the ibm-cloud-sdk-core package, but then the error is IamAuthenticator is not a constructor. How can we solve this?

1
What Node version are you on? How do you create the action? - data_henrik
I've tested it on version 12 and it's working but on version 10 - fails. I am aware of the fact that the support of version 10 will end soon, but it was given to me as a requirement to write the function in V10. Any ideas how to make it work on V10 - CodiClone

1 Answers

0
votes

See here for the runtime environments available in IBM Cloud Functions. Depending on the Node.js version, there are different versions of the IBM Watson SDK. As stated in the docs, the Node.js 12 runtime has the newer SDK with the IAM Authentication functionality you are looking for. The Node.js 10 runtime has the V4.x IBM Watson SDK.

Thus, you need to either use the old (documented) way of authenticating, or move up to the Node.js 12 runtime.

If you use the old username / password authentication and only have an IAM API key, use iamapikey as username and the API key as password.