Sathiesh Kumar
Sathiesh Kumar

Reputation: 61

How to solve this Error: Cannot find module 'ibm-watson'

I have installed ibm-watson using "npm install ibm-watson" command I can see the folder and its file in the node_modules folder, but still showing this error. Node version - v10.15.3

const watson = require('ibm-watson');
const { IamAuthenticator } = require('ibm-watson/auth');
const { BasicAuthenticator } = require('ibm-watson/auth');

// to get an IAM Access Token
const authorization = new watson.AuthorizationV1({
  authenticator: new IamAuthenticator({ apikey: 'fakekey-1234' }),
});

authorization.getToken(function (err, token) {
  if (!token) {
    console.log('error: ', err);
  } else {
    // Use your token here
  }
});

Other modules are importing fine, Only this module not importing.

internal/modules/cjs/loader.js:584
    throw err;
    ^

Error: Cannot find module 'ibm-watson'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous>

Upvotes: 2

Views: 2114

Answers (3)

Horea Porutiu
Horea Porutiu

Reputation: 176

I've just faced this issue. I did not install the correct version of the package. Please check the apidocs for Node to see the correct version of IBM Watson npm package that you need. for me I needed 5.6.0.

You can install it with the following command:

npm install ibm-watson@^5.6.0

Upvotes: 1

yukihirop
yukihirop

Reputation: 532

I faced the same problem. 

After reading the code, I understood.

There is only sdk.ts file, not index.ts file.

https://github.com/watson-developer-cloud/node-sdk

// const watson = require('ibm-watson');
const watson = require('ibm-watson/sdk');

But I still got the error.

Eventually it worked if I wrote the following

import AuthorizationV1 from 'ibm-watson/authorization/v1'
import { IamAuthenticator } from 'ibm-watson/auth'

const apikey = '********'
const authorization = new AuthorizationV1({
  url: 'https://iam.cloud.ibm.com/identity/token',
  authenticator: new IamAuthenticator({ apikey }),
})

authorization.getToken(function (err, token) {
  if (!token) {
    console.log('error: ', err);
  } else {
    // Use your token here
  }
});

But there is a CORS problem. I don't know any more.

enter image description here

The answer was written here. I need to do it on the server side

https://github.com/watson-developer-cloud/node-sdk/issues/884#issuecomment-515050023

Upvotes: 0

chughts
chughts

Reputation: 4737

As you are getting a token, I am going to guess that you are using Speech To Text. As the comments have suggested the failing line is const watson = require('ibm-watson'); because it isn't exported. Instead you would use, as per the API documentation - https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text?code=node#authentication:

const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const { IamTokenManager } = require('ibm-watson/auth'); 

If it's not STT that you are using then the other services work the same way when requiring ibm-watson. Links to the API Docs can be found here - https://cloud.ibm.com/apidocs

Upvotes: 0

Related Questions