0

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?

CodiClone
  • 141
  • 1
  • 2
  • 8
  • What Node version are you on? How do you create the action? – data_henrik Mar 07 '21 at 14:57
  • 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 Mar 07 '21 at 15:14

1 Answers1

0

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.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • I've read this but the problem is that the old documented way requires username and password and Discovery give us only apikey. – CodiClone Mar 08 '21 at 10:20
  • Just wanted to comment to mention that you aren't limited to only the NPM packages the Cloud Functions runtime provides, as long as you're willing to set up a build system like Webpack to bundle the exact module you want into the source code file you deploy. It's strange from the perspective of a Node.js dev because we're used to being able to specify the packages we want in package.json and then run npm install (or, we expect the cloud pattern were deploying to to be able to accept the package.json file and download the packages we want), but it's based on the underlying platform (OpenWhisk). – Matt Welke Mar 30 '21 at 05:42