简体   繁体   English

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

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

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.请检查 Node 的apidocs以查看您需要的 IBM Watson npm 包的正确版本。 for me I needed 5.6.0.对我来说,我需要 5.6.0。

You can install it with the following command:您可以使用以下命令安装它:

npm install ibm-watson@^5.6.0

As you are getting a token, I am going to guess that you are using Speech To Text.当您获得令牌时,我猜您正在使用 Speech To Text。 As the comments have suggested the failing line is const watson = require('ibm-watson');由于评论表明失败的行是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 :相反,您会根据 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'); 

If it's not STT that you are using then the other services work the same way when requiring ibm-watson .如果您使用的不是 STT,则其他服务在需要ibm-watson时以相同的方式工作。 Links to the API Docs can be found here - https://cloud.ibm.com/apidocs可以在此处找到 API 文档的链接 - https://cloud.ibm.com/apidocs

I faced the same problem.我遇到了同样的问题。

After reading the code, I understood.看完代码,我明白了。

There is only sdk.ts file, not index.ts file.只有sdk.ts文件,没有index.ts文件。

https://github.com/watson-developer-cloud/node-sdk 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.但是有一个 CORS 问题。 I don't know any more.我不知道了。

在此处输入图片说明

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 https://github.com/watson-developer-cloud/node-sdk/issues/884#issuecomment-515050023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有子目录的节点模块:“错误解析触发器:找不到模块‘ibm-watson’” - Node modules with sub-directories: "Error parsing triggers: Cannot find module 'ibm-watson'" 如何为使用 nodejs 的 Watson Assistant API 查找 ibm-watson 的 api-key 和密码 - How to find api-key and password for ibm-watson for Watson Assistant API working with nodejs 由于模块 ibm-watson/auth 无法运行代码 - not able to run the code due to module ibm-watson/auth is cant fund 如何解决错误:找不到模块&#39;kue&#39; - how to solve Error: Cannot find module 'kue' 如何解决错误:找不到模块“ejs”? - How to solve Error : cannot find module "ejs"? 如何解决错误:找不到模块&#39;json-parse-helpfulerror&#39; - How to solve error: Cannot find module 'json-parse-helpfulerror' 如何解决Node.js错误:找不到模块? - How to solve Node.js Error: Cannot find Module? 如何解决错误信息“找不到模块'缩写' - How to solve error message "Cannot find module 'abbrev' 使用 ibm watson 在聊天机器人中发送获取请求时如何解决错误? - How to solve an error while sending a get request in chatbot using ibm watson? 如何解决 angular 错误“发生未处理的异常:找不到模块'typescript'”? - How to solve angular error "An unhandled exception occurred: Cannot find module 'typescript' "?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM