如何解决此错误:找不到模块'ibm-watson'

问题描述 投票:1回答:2

我已经使用“ npm install ibm-watson”命令安装了ibm-watson我可以在node_modules文件夹中看到该文件夹​​及其文件,但仍显示此错误。节点版本-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
  }
});

其他模块正在导入,仅此模块未导入。

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>
node.js node-modules ibm-watson
2个回答
0
投票

[当您获得令牌时,我想您正在使用语音转文本。正如注释所建议的那样,失败的行是const watson = require('ibm-watson');,因为它没有被导出。相反,根据API文档,您将使用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'); 

如果您使用的不是STT,则在需要ibm-watson时其他服务的工作方式相同。可以在此处找到指向API文档的链接-https://cloud.ibm.com/apidocs


0
投票

我遇到了同样的问题。

阅读代码后,我了解了。

只有sdk.ts文件,没有index.ts文件。

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

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

但是我仍然遇到错误。

最终,如果我写了以下内容,它会起作用

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
  }
});

但是存在CORS问题。我不知道了。

enter image description here

答案写在这里。我需要在服务器端进行此操作

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

© www.soinside.com 2019 - 2024. All rights reserved.